<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Microsoft YaHei:300,300italic,400,400italic,700,700italic|consolas:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="tools,webpack," />








  <link rel="shortcut icon" type="image/x-icon" href="http://ofy7k3v4s.bkt.clouddn.com/mylogo.png?v=5.0.2" />






<meta name="description" content="CommonJS &amp;#x548C; AMD &amp;#x662F;&amp;#x7528;&amp;#x4E8E; JavaScript &amp;#x6A21;&amp;#x5757;&amp;#x7BA1;&amp;#x7406;&amp;#x7684;&amp;#x4E24;&amp;#x5927;&amp;#x89C4;&amp;#x8303;&amp;#xFF0C;&amp;#x524D;&amp;#x8005;&amp;#x5B9A;&amp;#x4E49;&amp;#x7684;&amp;#x662F;&amp;#x6A21;&amp;#x575">
<meta property="og:type" content="article">
<meta property="og:title" content="webpack 构建工具">
<meta property="og:url" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/index.html">
<meta property="og:site_name" content="HongqinMa' Blog">
<meta property="og:description" content="CommonJS &amp;#x548C; AMD &amp;#x662F;&amp;#x7528;&amp;#x4E8E; JavaScript &amp;#x6A21;&amp;#x5757;&amp;#x7BA1;&amp;#x7406;&amp;#x7684;&amp;#x4E24;&amp;#x5927;&amp;#x89C4;&amp;#x8303;&amp;#xFF0C;&amp;#x524D;&amp;#x8005;&amp;#x5B9A;&amp;#x4E49;&amp;#x7684;&amp;#x662F;&amp;#x6A21;&amp;#x575">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack001.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack002.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack003.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack004.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack005.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack006.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack007.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack008.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack009.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack010.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack011.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack012.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack013.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack014.png">
<meta property="og:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack015.png">
<meta property="og:updated_time" content="2016-12-15T03:20:30.798Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="webpack 构建工具">
<meta name="twitter:description" content="CommonJS &amp;#x548C; AMD &amp;#x662F;&amp;#x7528;&amp;#x4E8E; JavaScript &amp;#x6A21;&amp;#x5757;&amp;#x7BA1;&amp;#x7406;&amp;#x7684;&amp;#x4E24;&amp;#x5927;&amp;#x89C4;&amp;#x8303;&amp;#xFF0C;&amp;#x524D;&amp;#x8005;&amp;#x5B9A;&amp;#x4E49;&amp;#x7684;&amp;#x662F;&amp;#x6A21;&amp;#x575">
<meta name="twitter:image" content="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/webpack001.png">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '6347850919329138000',
      author: 'Hiraku'
    }
  };
</script>




  <link rel="canonical" href="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/"/>


  <title> webpack 构建工具 | HongqinMa' Blog </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="en">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?10010323";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta custom-logo">
  
    <div class="site-meta-headline">
      <a>
        <img class="custom-logo-image" src="http://ofy7k3v4s.bkt.clouddn.com/photo.jpg"
             alt="HongqinMa' Blog"/>
      </a>
    </div>
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">HongqinMa' Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">Spell at all costs， to rush you in the future。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                webpack 构建工具
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2016-12-02T21:21:03+08:00" content="2016-12-02">
              2016-12-02
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/webpack/" itemprop="url" rel="index">
                    <span itemprop="name">webpack</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2016/12/02/t13-webpack项目构建工具/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/12/02/t13-webpack项目构建工具/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          

          
              &nbsp; | &nbsp;
              <span class="page-pv"><i class="fa fa-file-o"></i>
              <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
              </span>
          

          
            <span id="busuanzi_container_page_pv">  <span id="busuanzi_value_page_pv"></span> Views</span>
          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>CommonJS &#x548C; AMD &#x662F;&#x7528;&#x4E8E; JavaScript &#x6A21;&#x5757;&#x7BA1;&#x7406;&#x7684;&#x4E24;&#x5927;&#x89C4;&#x8303;&#xFF0C;&#x524D;&#x8005;&#x5B9A;&#x4E49;&#x7684;&#x662F;&#x6A21;&#x5757;&#x7684;&#x540C;&#x6B65;&#x52A0;&#x8F7D;&#xFF0C;&#x4E3B;&#x8981;&#x7528;&#x4E8E; NodeJS &#xFF1B;&#x800C;&#x540E;&#x8005;&#x5219;&#x662F;&#x5F02;&#x6B65;&#x52A0;&#x8F7D;&#xFF0C;&#x901A;&#x8FC7; RequireJS &#x7B49;&#x5DE5;&#x5177;&#x9002;&#x7528;&#x4E8E;&#x524D;&#x7AEF;&#x3002;&#x968F;&#x7740; npm &#x6210;&#x4E3A;&#x4E3B;&#x6D41;&#x7684; JavaScript &#x7EC4;&#x4EF6;&#x53D1;&#x5E03;&#x5E73;&#x53F0;&#xFF0C;&#x8D8A;&#x6765;&#x8D8A;&#x591A;&#x7684;&#x524D;&#x7AEF;&#x9879;&#x76EE;&#x4E5F;&#x4F9D;&#x8D56;&#x4E8E; npm &#x4E0A;&#x7684;&#x9879;&#x76EE;&#xFF0C;&#x6216;&#x8005;&#x81EA;&#x8EAB;&#x5C31;&#x4F1A;&#x53D1;&#x5E03;&#x5230; npm &#x5E73;&#x53F0;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x8BA9;&#x524D;&#x7AEF;&#x9879;&#x76EE;&#x66F4;&#x65B9;&#x4FBF;&#x7684;&#x4F7F;&#x7528; npm &#x4E0A;&#x7684;&#x8D44;&#x6E90;&#x6210;&#x4E3A;&#x4E00;&#x5927;&#x9700;&#x6C42;&#x3002;</p>
<p>web &#x5F00;&#x53D1;&#x4E2D;&#x5E38;&#x7528;&#x5230;&#x7684;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x4E3B;&#x8981;&#x6709; JavaScript&#x3001;CSS&#x3001;&#x56FE;&#x7247;&#x3001;Jade &#x7B49;&#x6587;&#x4EF6;&#xFF0C;webpack &#x4E2D;&#x5C06;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x6587;&#x4EF6;&#x79F0;&#x4E4B;&#x4E3A;&#x6A21;&#x5757;&#x3002; webpack &#x662F;&#x4E00;&#x4E2A; module bundler (&#x6A21;&#x5757;&#x6253;&#x5305;&#x5DE5;&#x5177;)&#xFF0C;&#x5176;&#x53EF;&#x4EE5;&#x517C;&#x5BB9;&#x591A;&#x79CD; js &#x4E66;&#x5199;&#x89C4;&#x8303;&#xFF0C;&#x4E14;&#x53EF;&#x4EE5;&#x5904;&#x7406;&#x6A21;&#x5757;&#x95F4;&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#xFF0C;&#x5177;&#x6709;&#x66F4;&#x5F3A;&#x5927;&#x7684; js &#x6A21;&#x5757;&#x5316;&#x7684;&#x529F;&#x80FD;&#x3002;Webpack &#x5BF9;&#x5B83;&#x4EEC;&#x8FDB;&#x884C;&#x7EDF;&#x4E00;&#x7684;&#x7BA1;&#x7406;&#x4EE5;&#x53CA;&#x6253;&#x5305;&#x53D1;&#x5E03;&#xFF0C;&#x5176;&#x5B98;&#x65B9;&#x4E3B;&#x9875;&#x7528;&#x4E0B;&#x9762;&#x8FD9;&#x5F20;&#x56FE;&#x6765;&#x8BF4;&#x660E; Webpack &#x7684;&#x4F5C;&#x7528;.</p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack001.png" alt="webpack&#x7684;&#x4F5C;&#x7528;"></p>
<h2 id="webpack-&#x4ECB;&#x7ECD;"><a href="#webpack-&#x4ECB;&#x7ECD;" class="headerlink" title="webpack &#x4ECB;&#x7ECD;"></a>webpack &#x4ECB;&#x7ECD;</h2><p>webpack &#x66F4; Gulp &#x7684;&#x4F5C;&#x7528;&#x76F8;&#x540C;&#xFF0C;&#x662F;&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;&#x3002;</p>
<h3 id="webpack-&#x548C;-Gulp-&#x7684;&#x533A;&#x522B;"><a href="#webpack-&#x548C;-Gulp-&#x7684;&#x533A;&#x522B;" class="headerlink" title="webpack &#x548C; Gulp &#x7684;&#x533A;&#x522B;"></a>webpack &#x548C; Gulp &#x7684;&#x533A;&#x522B;</h3><blockquote>
<p>Gulp &#x51FA;&#x73B0;&#x7684;&#x6BD4;&#x8F83;&#x65E9;&#xFF0C;&#x66F4;&#x9002;&#x5408;&#x4E8E;&#x505A;&#x4EFB;&#x52A1;&#x578B;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;&#x5904;&#x7406;&#x4EFB;&#x4F55;&#x7684;&#x7F51;&#x7AD9;&#x9759;&#x6001;&#x7F51;&#x7AD9;&#x3001;SPA&#x3001;Node.js &#x9879;&#x76EE;&#x4EE3;&#x7801;&#xFF0C;Gulp &#x91CC;&#x9762;&#x5C31;&#x662F;&#x4E00;&#x5806;&#x7684;&#x4EFB;&#x52A1;&#xFF1B;<br>Webpack &#x4E00;&#x822C;&#x5168;&#x90E8;&#x7528;&#x6765;&#x5904;&#x7406; SPA &#x5E94;&#x7528;&#xFF0C;&#x5C31; React&#x3001;Vue.js&#x3001;AngularJS &#x4F7F;&#x7528;&#x3002;</p>
</blockquote>
<p>&#x6240;&#x4EE5;&#x4F7F;&#x7528;&#x7684;&#x573A;&#x666F;&#x4E0D;&#x4E00;&#x6837;&#xFF0C;&#x56E0;&#x4E3A;&#x5185;&#x90E8;&#x7684;&#x539F;&#x7406;&#x4E0D;&#x540C;&#x3002;</p>
<h3 id="webpack-&#x5B98;&#x7F51;&#x6587;&#x6863;"><a href="#webpack-&#x5B98;&#x7F51;&#x6587;&#x6863;" class="headerlink" title="webpack &#x5B98;&#x7F51;&#x6587;&#x6863;"></a>webpack &#x5B98;&#x7F51;&#x6587;&#x6863;</h3><p>&#x5B98;&#x7F51;&#x5730;&#x5740;&#xFF1A;<a href="http://webpack.github.io/docs/" target="_blank" rel="external">http://webpack.github.io/docs/</a></p>
<h3 id="webpack-&#x7684;&#x4F18;&#x52BF;"><a href="#webpack-&#x7684;&#x4F18;&#x52BF;" class="headerlink" title="webpack &#x7684;&#x4F18;&#x52BF;"></a>webpack &#x7684;&#x4F18;&#x52BF;</h3><blockquote>
<p>&#x5BF9; CommonJS &#x3001; AMD &#x3001;ES6 &#x7684;&#x8BED;&#x6CD5;&#x505A;&#x4E86;&#x517C;&#x5BB9;<br>&#x5BF9; js&#x3001;css&#x3001;&#x56FE;&#x7247;&#x7B49;&#x8D44;&#x6E90;&#x6587;&#x4EF6;&#x90FD;&#x652F;&#x6301;&#x6253;&#x5305;<br>&#x4E32;&#x8054;&#x5F0F;&#x6A21;&#x5757;&#x52A0;&#x8F7D;&#x5668;&#x4EE5;&#x53CA;&#x63D2;&#x4EF6;&#x673A;&#x5236;&#xFF0C;&#x8BA9;&#x5176;&#x5177;&#x6709;&#x66F4;&#x597D;&#x7684;&#x7075;&#x6D3B;&#x6027;&#x548C;&#x6269;&#x5C55;&#x6027;&#xFF0C;&#x4F8B;&#x5982;&#x63D0;&#x4F9B;&#x5BF9; CoffeeScript&#x3001;ES6&#x7684;&#x652F;&#x6301;<br>&#x6709;&#x72EC;&#x7ACB;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6; webpack.config.js<br>&#x53EF;&#x4EE5;&#x5C06;&#x4EE3;&#x7801;&#x5207;&#x5272;&#x6210;&#x4E0D;&#x540C;&#x7684; chunk&#xFF0C;&#x5B9E;&#x73B0;&#x6309;&#x9700;&#x52A0;&#x8F7D;&#xFF0C;&#x964D;&#x4F4E;&#x4E86;&#x521D;&#x59CB;&#x5316;&#x65F6;&#x95F4;<br>&#x652F;&#x6301; SourceUrls &#x548C; SourceMaps&#xFF0C;&#x6613;&#x4E8E;&#x8C03;&#x8BD5;<br>&#x5177;&#x6709;&#x5F3A;&#x5927;&#x7684; Plugin &#x63A5;&#x53E3;&#xFF0C;&#x5927;&#x591A;&#x662F;&#x5185;&#x90E8;&#x63D2;&#x4EF6;&#xFF0C;&#x4F7F;&#x7528;&#x8D77;&#x6765;&#x6BD4;&#x8F83;&#x7075;&#x6D3B;<br>webpack &#x4F7F;&#x7528;&#x5F02;&#x6B65; IO &#x5E76;&#x5177;&#x6709;&#x591A;&#x7EA7;&#x7F13;&#x5B58;&#x3002;&#x8FD9;&#x4F7F;&#x5F97; webpack &#x5F88;&#x5FEB;&#x4E14;&#x5728;&#x589E;&#x91CF;&#x7F16;&#x8BD1;&#x4E0A;&#x66F4;&#x52A0;&#x5FEB;</p>
</blockquote>
<h2 id="webpack-&#x7684;&#x4F7F;&#x7528;"><a href="#webpack-&#x7684;&#x4F7F;&#x7528;" class="headerlink" title="webpack &#x7684;&#x4F7F;&#x7528;"></a>webpack &#x7684;&#x4F7F;&#x7528;</h2><p>&#x65B0;&#x5EFA;&#x9879;&#x76EE;</p>
<p>&#x5728;&#x9879;&#x76EE;&#x6839;&#x76EE;&#x5F55;&#x4E0B;&#x8FD0;&#x884C;&#xFF1A;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm init -y</div></pre></td></tr></table></figure>
<h3 id="&#x642D;&#x5EFA;&#x57FA;&#x672C;&#x7684;&#x9879;&#x76EE;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;"><a href="#&#x642D;&#x5EFA;&#x57FA;&#x672C;&#x7684;&#x9879;&#x76EE;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;" class="headerlink" title="&#x642D;&#x5EFA;&#x57FA;&#x672C;&#x7684;&#x9879;&#x76EE;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;"></a>&#x642D;&#x5EFA;&#x57FA;&#x672C;&#x7684;&#x9879;&#x76EE;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;</h3><p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack002.png" alt="&#x9879;&#x76EE;&#x7ED3;&#x6784;&#x76EE;&#x5F55;"></p>
<p>src &#x4E2D;&#x7684;&#x5F00;&#x53D1;&#x6587;&#x4EF6;&#xFF0C;dist &#x662F;&#x6253;&#x5305;&#x540E;&#x7684;&#x6587;&#x4EF6;</p>
<h3 id="&#x5B89;&#x88C5;"><a href="#&#x5B89;&#x88C5;" class="headerlink" title="&#x5B89;&#x88C5;"></a>&#x5B89;&#x88C5;</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$ npm install webpack -g</div><div class="line">$ npm install webpack -save-dev</div><div class="line">$ npm install react -save</div></pre></td></tr></table></figure>
<h3 id="&#x914D;&#x7F6E;&#x6587;&#x4EF6;"><a href="#&#x914D;&#x7F6E;&#x6587;&#x4EF6;" class="headerlink" title="&#x914D;&#x7F6E;&#x6587;&#x4EF6;"></a>&#x914D;&#x7F6E;&#x6587;&#x4EF6;</h3><blockquote>
<p>webpack.develop.config.js</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// webpack &#x7684;&#x5F00;&#x53D1;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</span></div><div class="line"><span class="comment">// &#x7F16;&#x5199;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x8981;&#x6709;&#x6700;&#x57FA;&#x672C;&#x7684;&#x6587;&#x4EF6;&#x5165;&#x53E3;&#x548C;&#x8F93;&#x51FA;&#x6587;&#x4EF6;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x7B49;</span></div><div class="line"><span class="comment">// &#x91CC;&#x9762;&#x8FD8;&#x53EF;&#x4EE5;&#x52A0;loader&#x548C;&#x5404;&#x79CD;&#x63D2;&#x4EF6;&#x914D;&#x7F6E;&#x4F7F;&#x7528;</span></div><div class="line"><span class="keyword">var</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"><span class="built_in">module</span>.exports = {</div><div class="line">    <span class="comment">// &#x5355;&#x9875;&#x9762; SPA &#x7684;&#x5165;&#x53E3;&#x6587;&#x4EF6;</span></div><div class="line">    entry:path.resolve(__dirname,<span class="string">&apos;src/js/app.js&apos;</span>),</div><div class="line">    <span class="comment">// &#x6784;&#x5EFA;&#x4E4B;&#x540E;&#x7684;&#x6587;&#x4EF6;&#x8F93;&#x51FA;&#x4F4D;&#x7F6E;&#x914D;&#x7F6E;</span></div><div class="line">    output: {</div><div class="line">        <span class="attr">path</span>: path.resolve(__dirname, <span class="string">&apos;dist&apos;</span>),</div><div class="line">        <span class="attr">filename</span>: <span class="string">&apos;bundle.js&apos;</span></div><div class="line">    }</div><div class="line">};</div></pre></td></tr></table></figure>
<h3 id="&#x8FD0;&#x884C;"><a href="#&#x8FD0;&#x884C;" class="headerlink" title="&#x8FD0;&#x884C;"></a>&#x8FD0;&#x884C;</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ webpack --config webpack.develop.config.js</div></pre></td></tr></table></figure>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack003.png" alt="&#x8FD0;&#x884C;&#x5B8C;&#x4E4B;&#x540E;&#x751F;&#x6210; bundle.js &#x6587;&#x4EF6;"></p>
<h3 id="&#x8FDB;&#x884C;&#x7248;&#x672C;&#x63A7;&#x5236;"><a href="#&#x8FDB;&#x884C;&#x7248;&#x672C;&#x63A7;&#x5236;" class="headerlink" title="&#x8FDB;&#x884C;&#x7248;&#x672C;&#x63A7;&#x5236;"></a>&#x8FDB;&#x884C;&#x7248;&#x672C;&#x63A7;&#x5236;</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">$ git init</div><div class="line">$ git status</div><div class="line">$ git add -A</div><div class="line">$ git commit -m <span class="string">&quot;&#x9879;&#x76EE;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x53CA; webpack &#x521D;&#x6B65;&#x914D;&#x7F6E;&quot;</span></div></pre></td></tr></table></figure>
<hr>
<h2 id="webpack-&#x542F;&#x52A8;&#x8FC7;&#x7A0B;&#x6F14;&#x8FDB;"><a href="#webpack-&#x542F;&#x52A8;&#x8FC7;&#x7A0B;&#x6F14;&#x8FDB;" class="headerlink" title="webpack &#x542F;&#x52A8;&#x8FC7;&#x7A0B;&#x6F14;&#x8FDB;"></a>webpack &#x542F;&#x52A8;&#x8FC7;&#x7A0B;&#x6F14;&#x8FDB;</h2><p>&#x628A;&#x8FD0;&#x884C;&#x547D;&#x4EE4;&#x914D;&#x7F6E;&#x5230; npm &#x7684; script &#x4E2D;&#x3002;  package.json</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="string">&quot;scripts&quot;</span>: {</div><div class="line">    <span class="string">&quot;test&quot;</span>: <span class="string">&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span>,</div><div class="line">    <span class="string">&quot;develop&quot;</span>: <span class="string">&quot;webpack --config webpack.develop.config.js&quot;</span>,</div><div class="line">    <span class="string">&quot;publish&quot;</span>: <span class="string">&quot;webpack --config webpack.publish.config.js&quot;</span></div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x6267;&#x884C; &#xFF1A;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm run develop</div></pre></td></tr></table></figure>
<h3 id="&#x66F4;&#x597D;&#x7684;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;&#x52A8;&#x542F;&#x52A8;"><a href="#&#x66F4;&#x597D;&#x7684;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;&#x52A8;&#x542F;&#x52A8;" class="headerlink" title="&#x66F4;&#x597D;&#x7684;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;&#x52A8;&#x542F;&#x52A8;"></a>&#x66F4;&#x597D;&#x7684;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;&#x52A8;&#x542F;&#x52A8;</h3><p>&#x5982;&#x679C;&#x9700;&#x8981;&#x4E00;&#x76F4;&#x8F93;&#x5165; npm run develop &#x786E;&#x5B9E;&#x662F;&#x4E00;&#x4EF6;&#x975E;&#x5E38;&#x65E0;&#x804A;&#x7684;&#x4E8B;&#x60C5;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x628A;&#x8BA9;&#x4ED6;&#x5B89;&#x9759;&#x7684;&#x8FD0;&#x884C;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x8BBE;&#x7F6E; webpack-dev-server</p>
<p>&#x9664;&#x4E86;&#x63D0;&#x4F9B;&#x6A21;&#x5757;&#x6253;&#x5305;&#x529F;&#x80FD;&#xFF0C;Webpack &#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x57FA;&#x4E8E; Node.js Express &#x6846;&#x67B6;&#x7684;&#x5F00;&#x53D1;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x9759;&#x6001;&#x8D44;&#x6E90; Web &#x670D;&#x52A1;&#x5668;&#xFF0C;&#x5BF9;&#x4E8E;&#x7B80;&#x5355;&#x9759;&#x6001;&#x9875;&#x9762;&#x6216;&#x8005;&#x4EC5;&#x4F9D;&#x8D56;&#x4E8E;&#x72EC;&#x7ACB;&#x670D;&#x52A1;&#x7684;&#x524D;&#x7AEF;&#x9875;&#x9762;&#xFF0C;&#x90FD;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x5F00;&#x53D1;&#x670D;&#x52A1;&#x5668;&#x8FDB;&#x884C;&#x5F00; &#x53D1;&#x3002;&#x5728;&#x5F00;&#x53D1;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x5F00;&#x53D1;&#x670D;&#x52A1;&#x5668;&#x4F1A;&#x76D1;&#x542C;&#x6BCF;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x8FDB;&#x884C;&#x5B9E;&#x65F6;&#x6253;&#x5305;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x63A8;&#x9001;&#x901A;&#x77E5;&#x524D;&#x7AEF;&#x9875;&#x9762;&#x4EE3;&#x7801;&#x53D1;&#x751F;&#x4E86;&#x53D8;&#x5316;&#xFF0C;&#x4ECE;&#x800C;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x9875;&#x9762;&#x7684;&#x81EA;&#x52A8;&#x5237;&#x65B0;&#x3002;</p>
<p>&#x66F4;&#x597D;&#x7684;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;&#x81EA;&#x52A8;&#x542F;&#x52A8;&#xFF1A;webpack &#x5B98;&#x65B9;&#x63D0;&#x4F9B;&#x7684;&#x4E00;&#x4E2A;&#x7B2C;&#x4E09;&#x4E2A;&#x7684;&#x63D2;&#x4EF6;&#xFF0C;&#x81EA;&#x52A8;&#x76D1;&#x542C;&#x4EE3;&#x7801;&#x53D8;&#x5316;&#xFF0C;&#x5E2E;&#x6211;&#x4EEC;&#x91CD;&#x65B0;&#x6784;&#x5EFA;&#xFF0C;&#x628A; webpack &#x548C; express &#x5C01;&#x88C5;&#x4E86;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install webpack-dev-server -save-dev</div></pre></td></tr></table></figure>
<p>&#x8C03;&#x6574; npm &#x7684; package.json scripts &#x90E8;&#x5206;&#x4E2D;&#x5F00;&#x53D1;&#x547D;&#x4EE4;&#x7684;&#x914D;&#x7F6E;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="string">&quot;scripts&quot;</span>: {</div><div class="line">  <span class="string">&quot;test&quot;</span>: <span class="string">&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span>,</div><div class="line">  <span class="string">&quot;develop&quot;</span>: <span class="string">&quot;webpack-dev-server --config webpack.develop.config.js --devtool eval --progress --colors --hot --content-base src&quot;</span>,</div><div class="line">  <span class="string">&quot;publish&quot;</span>: <span class="string">&quot;webpack --config webpack.publish.config.js&quot;</span></div><div class="line">}</div></pre></td></tr></table></figure>
<blockquote>
<p>webpack-dev-server - &#x5728; localhost:8080 &#x5EFA;&#x7ACB;&#x4E00;&#x4E2A; Web &#x670D;&#x52A1;&#x5668;<br>&#x2013;devtool eval - &#x4E3A;&#x4F60;&#x7684;&#x4EE3;&#x7801;&#x521B;&#x5EFA;&#x6E90;&#x5730;&#x5740;&#x3002;&#x5F53;&#x6709;&#x4EFB;&#x4F55;&#x62A5;&#x9519;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x8BA9;&#x4F60;&#x66F4;&#x52A0;&#x7CBE;&#x786E;&#x5730;&#x5B9A;&#x4F4D;&#x5230;&#x6587;&#x4EF6;&#x548C;&#x884C;&#x53F7;<br>&#x2013;progress - &#x663E;&#x793A;&#x5408;&#x5E76;&#x4EE3;&#x7801;&#x8FDB;&#x5EA6;<br>&#x2013;colors &#x2013; hot&#xFF0C;&#x547D;&#x4EE4;&#x884C;&#x4E2D;&#x663E;&#x793A;&#x989C;&#x8272;&#xFF01;<br>&#x2013;content-base  &#x6307;&#x5411;&#x8BBE;&#x7F6E;&#x7684;&#x8F93;&#x51FA;&#x76EE;&#x5F55;//&#x8FD9;&#x70B9;&#x4E00;&#x5B9A;&#x662F;&#x6211;&#x4EEC;&#x7684;&#x53D1;&#x5E03;&#x76EE;&#x5F55;</p>
</blockquote>
<p>&#x5728; src &#x4E0B;&#x9762;&#xFF0C;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A; index.html &#x6587;&#x4EF6;&#xFF0C;</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>webpack &#x4F7F;&#x7528;<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;bundle.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<p>&#x6267;&#x884C;<code>npm run develop</code> &#xFF0C;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;</p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack004.png" alt="&#x6267;&#x884C; npm run develop"></p>
<p>&#x6267;&#x884C; <code>npm run develop</code> &#x4E4B;&#x540E;&#x6211;&#x4EEC;&#x53D1;&#x73B0;&#x6267;&#x884C;&#x6CA1;&#x6709;&#x7ED3;&#x675F;&#xFF0C;&#x542F;&#x52A8;&#x7740;&#x76D1;&#x542C;&#xFF0C;&#x5E76;&#x5728; 8080 &#x7AEF;&#x53E3;&#x5F00;&#x542F;&#x4E86;&#x4E00;&#x4E2A;&#x670D;&#x52A1;&#x5668;&#x3002;</p>
<p>&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x6253;&#x5F00;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack005.png" alt="&#x6D4F;&#x89C8;&#x5668;&#x6253;&#x5F00;&#x7ED3;&#x679C;"></p>
<p>&#x5982;&#x679C;&#x4FEE;&#x6539;&#x4E86; app.js &#x6587;&#x4EF6;&#xFF0C;&#x4F1A;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x6784;&#x5EFA;&#xFF0C;&#x5237;&#x65B0;&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x3002;</p>
<p><strong>&#x5728; index.html &#x8BBF;&#x95EE;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x8BBF;&#x95EE;  bundle.js &#x6587;&#x4EF6;&#xFF0C;&#x4E3A;&#x4EC0;&#x4E48;&#xFF0C;&#x56E0;&#x4E3A; webpack-dev-server &#x751F;&#x6210;&#x7684; bundle &#x5728;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x653E;&#x5230;&#x5185;&#x5B58;&#x4E2D;&#x6784;&#x5EFA;&#x5FEB;</strong></p>
<p>&#x603B;&#x7684;&#x6765;&#x8BF4;&#xFF0C;&#x5F53;&#x4F60;&#x8FD0;&#x884C; npm run develop &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x542F;&#x52A8;&#x4E00;&#x4E2A; Web &#x670D;&#x52A1;&#x5668;&#xFF0C;&#x7136;&#x540E;&#x76D1;&#x542C;&#x6587;&#x4EF6;&#x4FEE;&#x6539;&#xFF0C;&#x7136;&#x540E;&#x81EA;&#x52A8;&#x91CD;&#x65B0;&#x5408;&#x5E76;&#x4F60;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x771F;&#x7684;&#x975E;&#x5E38;&#x7B80;&#x6D01;&#xFF01;</p>
<p>&#x6CE8;&#x610F;:</p>
<blockquote>
<p>&#x7528; webpack-dev-server &#x751F;&#x6210; bundle.js &#x6587;&#x4EF6;&#x662F;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x7684;&#xFF0C;&#x5E76;&#x6CA1;&#x6709;&#x5B9E;&#x9645;&#x751F;&#x6210;<br>&#x5982;&#x679C;&#x5F15;&#x7528;&#x7684;&#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x5DF2;&#x7ECF;&#x6709; bundle.js &#x5C31;&#x4E0D;&#x4F1A;&#x81EA;&#x52A8;&#x5237;&#x65B0;&#x4E86;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x5148;&#x628A; bundle.js &#x6587;&#x4EF6;&#x624B;&#x52A8;&#x5220;&#x9664;<br>&#x7528; webstorm &#x9700;&#x8981;&#x6CE8;&#x610F;&#xFF0C;&#x56E0;&#x4E3A;&#x4ED6;&#x662F;&#x81EA;&#x52A8;&#x4FDD;&#x5B58;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x80FD;&#x8BC6;&#x522B;&#x7684;&#x6BD4;&#x8F83;&#x6162;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x624B;&#x52A8;&#x7684; ctrl+s &#x4E00;&#x4E0B;</p>
</blockquote>
<h3 id="&#x6D4F;&#x89C8;&#x5668;&#x81EA;&#x52A8;&#x5237;&#x65B0;"><a href="#&#x6D4F;&#x89C8;&#x5668;&#x81EA;&#x52A8;&#x5237;&#x65B0;" class="headerlink" title="&#x6D4F;&#x89C8;&#x5668;&#x81EA;&#x52A8;&#x5237;&#x65B0;"></a>&#x6D4F;&#x89C8;&#x5668;&#x81EA;&#x52A8;&#x5237;&#x65B0;</h3><p>&#x4FEE;&#x6539; webpack.develop.config.js &#x7684;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x914D;&#x7F6E;&#xFF0C;&#x4FEE;&#x6539; entry &#x90E8;&#x5206;&#x5982;&#x4E0B;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"><span class="built_in">module</span>.exports = {</div><div class="line">    <span class="comment">// &#x5355;&#x9875;&#x9762; SPA &#x7684;&#x5165;&#x53E3;&#x6587;&#x4EF6;</span></div><div class="line">    entry:[</div><div class="line">        <span class="comment">// &#x5B9E;&#x73B0;&#x6D4F;&#x89C8;&#x5668;&#x81EA;&#x52A8;&#x5237;&#x65B0;</span></div><div class="line">        <span class="string">&apos;webpack/hot/dev-server&apos;</span>,</div><div class="line">        <span class="string">&apos;webpack-dev-server/client?http://localhost:8080&apos;</span>,</div><div class="line">        path.resolve(__dirname,<span class="string">&apos;src/js/app.js&apos;</span>)</div><div class="line">    ],</div><div class="line">    <span class="comment">// &#x6784;&#x5EFA;&#x4E4B;&#x540E;&#x7684;&#x6587;&#x4EF6;&#x8F93;&#x51FA;&#x4F4D;&#x7F6E;&#x914D;&#x7F6E;</span></div><div class="line">    output: {</div><div class="line">        <span class="attr">path</span>: path.resolve(__dirname, <span class="string">&apos;dist&apos;</span>),</div><div class="line">        <span class="attr">filename</span>: <span class="string">&apos;bundle.js&apos;</span></div><div class="line">    }</div><div class="line">};</div></pre></td></tr></table></figure>
<p>&#x4FEE;&#x6539;&#x4E86;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x91CD;&#x65B0;&#x542F;&#x52A8;&#xFF0C;&#x6267;&#x884C; <code>npm run develop</code> &#x7ED3;&#x679C;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;</p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack006.png" alt="&#x7EC8;&#x7AEF;&#x6267;&#x884C;&#x7ED3;&#x679C;"></p>
<p>&#x6B64;&#x65F6;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;</p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack007.png" alt="&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x53D8;&#x5316;"></p>
<hr>
<h2 id="&#x5E38;&#x7528;&#x52A0;&#x8F7D;&#x5668;"><a href="#&#x5E38;&#x7528;&#x52A0;&#x8F7D;&#x5668;" class="headerlink" title="&#x5E38;&#x7528;&#x52A0;&#x8F7D;&#x5668;"></a>&#x5E38;&#x7528;&#x52A0;&#x8F7D;&#x5668;</h2><p>Loader&#xFF1A;&#x8FD9;&#x662F;webpack&#x51C6;&#x5907;&#x7684;&#x4E00;&#x4E9B;&#x9884;&#x5904;&#x7406;&#x5DE5;&#x5177;</p>
<p>&#x5728;&#x6784;&#x5EFA;&#x9879;&#x76EE;&#x4E4B;&#x524D;&#x505A;&#x4E00;&#x4E9B;&#x9884;&#x5904;&#x7406;&#x64CD;&#x4F5C;&#xFF0C;&#x6BD4;&#x5982; ES6 &#x8F6C; ES5&#xFF0C;Sass&#x3001;Less</p>
<h3 id="&#x7F16;&#x8BD1;-JSX-&#x548C;-ES6-&#x5230;-ES5-&#x8BED;&#x6CD5;&#x7684;&#x52A0;&#x8F7D;&#x5668;"><a href="#&#x7F16;&#x8BD1;-JSX-&#x548C;-ES6-&#x5230;-ES5-&#x8BED;&#x6CD5;&#x7684;&#x52A0;&#x8F7D;&#x5668;" class="headerlink" title="&#x7F16;&#x8BD1; JSX &#x548C; ES6 &#x5230; ES5 &#x8BED;&#x6CD5;&#x7684;&#x52A0;&#x8F7D;&#x5668;"></a>&#x7F16;&#x8BD1; JSX &#x548C; ES6 &#x5230; ES5 &#x8BED;&#x6CD5;&#x7684;&#x52A0;&#x8F7D;&#x5668;</h3><p>&#x5B89;&#x88C5;&#xFF1A;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">$ npm install babel-loader --save-dev</div><div class="line">$ npm install babel-core babel-preset-es2015 babel-preset-react --save-dev</div></pre></td></tr></table></figure>
<p>babel-loader: &#x8F6C;&#x6362;&#x5668;&#xFF0C;&#x7F16;&#x8BD1; JSX &#x8BED;&#x6CD5;&#x548C; ES6 &#x8BED;&#x6CD5;&#x5230; ES5 &#x8BED;&#x6CD5;&#x3002;</p>
<p>&#x4FEE;&#x6539;&#x5F00;&#x53D1;&#x914D;&#x7F6E;&#x73AF;&#x5883;: webpack.develop.config.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">module</span>: {</div><div class="line">    <span class="attr">loaders</span>: [</div><div class="line">        {</div><div class="line">            <span class="attr">test</span>: <span class="regexp">/\.jsx?$/</span>, <span class="comment">// &#x7528;&#x6B63;&#x5219;&#x6765;&#x5339;&#x914D;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#xFF0C;&#x8FD9;&#x6BB5;&#x610F;&#x601D;&#x662F;&#x5339;&#x914D; js &#x6216;&#x8005; jsx</span></div><div class="line">            loader: <span class="string">&apos;babel&apos;</span>, <span class="comment">// &#x52A0;&#x8F7D;&#x6A21;&#x5757; &quot;babel&quot; &#x662F; &quot;babel-loader&quot; &#x7684;&#x7F29;&#x5199;</span></div><div class="line">            query: {</div><div class="line">                <span class="attr">presets</span>: [<span class="string">&apos;es2015&apos;</span>, <span class="string">&apos;react&apos;</span>]</div><div class="line">            }</div><div class="line">        }</div><div class="line">    ]</div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x4E00;&#x4E2A; React Hello, World! app.js &#x6587;&#x4EF6;</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">// &#x9879;&#x76EE;&#x5165;&#x53E3;&#x6587;&#x4EF6;</div><div class="line">import React, {Component} from &apos;react&apos;;</div><div class="line">import ReactDOM from &apos;react-dom&apos;;</div><div class="line">ReactDOM.render(</div><div class="line">    &lt;div&gt;</div><div class="line">        Hello World!</div><div class="line">    &lt;/div&gt;,</div><div class="line">    document.getElementById(&apos;app&apos;)</div><div class="line">);</div></pre></td></tr></table></figure>
<h3 id="&#x52A0;&#x8F7D;-CSS"><a href="#&#x52A0;&#x8F7D;-CSS" class="headerlink" title="&#x52A0;&#x8F7D; CSS"></a>&#x52A0;&#x8F7D; CSS</h3><p>webpack &#x5141;&#x8BB8;&#x50CF;&#x52A0;&#x8F7D;&#x4EFB;&#x4F55;&#x4EE3;&#x7801;&#x4E00;&#x6837;&#x52A0;&#x8F7D; CSS&#x3002;&#x53EF;&#x4EE5;&#x9009;&#x62E9;&#x9700;&#x8981;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x4F46;&#x662F;&#x53EF;&#x4EE5;&#x4E3A;&#x6BCF;&#x4E2A;&#x7EC4;&#x4EF6;&#x628A;&#x6240;&#x6709;&#x7684; CSS &#x52A0;&#x8F7D;&#x5230;&#x5165;&#x53E3;&#x4E3B;&#x6587;&#x4EF6;&#x4E2D;&#x6765;&#x505A;&#x4EFB;&#x4F55;&#x4E8B;&#x60C5;&#x3002;</p>
<p>&#x52A0;&#x8F7D; CSS &#x9700;&#x8981; css-loader &#x548C; style-loader&#xFF0C;&#x4ED6;&#x4EEC;&#x505A;&#x4E24;&#x4EF6;&#x4E0D;&#x540C;&#x7684;&#x4E8B;&#x60C5;:</p>
<blockquote>
<p>css-loader &#x4F1A;&#x904D;&#x5386; CSS &#x6587;&#x4EF6;&#xFF0C;&#x7136;&#x540E;&#x627E;&#x5230; url() &#x8868;&#x8FBE;&#x5F0F;&#x7136;&#x540E;&#x5904;&#x7406;&#x4ED6;&#x4EEC;<br>style-loader &#x4F1A;&#x628A;&#x539F;&#x6765;&#x7684; CSS &#x4EE3;&#x7801;&#x63D2;&#x5165;&#x9875;&#x9762;&#x4E2D;&#x7684;&#x4E00;&#x4E2A; style &#x6807;&#x7B7E;&#x4E2D;</p>
</blockquote>
<h4 id="&#x5B89;&#x88C5;-1"><a href="#&#x5B89;&#x88C5;-1" class="headerlink" title="&#x5B89;&#x88C5;"></a>&#x5B89;&#x88C5;</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install css-loader style-loader --save-dev</div></pre></td></tr></table></figure>
<p>&#x65B0;&#x5EFA;&#x6587;&#x4EF6;&#x5939;&#xFF1A;components</p>
<p>&#x65B0;&#x589E;&#xFF1A;_base.css Hello.css Hello.js  Hello.sass &#x6587;&#x4EF6;</p>
<h4 id="&#x4FEE;&#x6539;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF1A;"><a href="#&#x4FEE;&#x6539;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF1A;" class="headerlink" title="&#x4FEE;&#x6539;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF1A;"></a>&#x4FEE;&#x6539;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF1A;</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x53EF;&#x4EE5;&#x5728; js &#x4E2D;&#x5F15;&#x7528; css &#x7684;&#x52A0;&#x8F7D;&#x5668;</span></div><div class="line">{</div><div class="line">    <span class="attr">test</span>: <span class="regexp">/\.css$/</span>,</div><div class="line">    <span class="attr">loader</span>: <span class="string">&apos;style!css&apos;</span> <span class="comment">// &#x5982;&#x679C;&#x540C;&#x65F6;&#x4F7F;&#x7528;&#x591A;&#x4E2A;&#x52A0;&#x8F7D;&#x5668;&#xFF0C;&#x4E2D;&#x95F4;&#x7528; ! &#x8FDE;&#x63A5;&#xFF0C;&#x52A0;&#x8F7D;&#x5668;&#x7684;&#x6267;&#x884C;&#x987A;&#x5E8F;&#x662F;&#x4ECE;&#x53F3;&#x5411;&#x5DE6;</span></div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#xFF01;&#x7528;&#x6765;&#x5B9A;&#x4E49;loader&#x7684;&#x4E32;&#x8054;&#x5173;&#x7CFB;&#xFF0C;&#x201D;-loader&#x201D;&#x662F;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x4E0D;&#x5199;&#x7684;&#xFF0C;&#x591A;&#x4E2A;loader&#x4E4B;&#x95F4;&#x7528;&#x201C;!&#x201D;&#x8FDE;&#x63A5;&#x8D77;&#x6765;</p>
<h4 id="Css&#x52A0;&#x8F7D;&#x7B56;&#x7565;"><a href="#Css&#x52A0;&#x8F7D;&#x7B56;&#x7565;" class="headerlink" title="Css&#x52A0;&#x8F7D;&#x7B56;&#x7565;"></a>Css&#x52A0;&#x8F7D;&#x7B56;&#x7565;</h4><p>1&#x3001;&#x5728;&#x4E3B;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x6BD4;&#x5982; src/app.js &#x4F60;&#x53EF;&#x4EE5;&#x4E3A;&#x6574;&#x4E2A;&#x9879;&#x76EE;&#x52A0;&#x8F7D;&#x6240;&#x6709;&#x7684; CSS</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span>  <span class="string">&apos;./project-styles.css&apos;</span>;</div></pre></td></tr></table></figure>
<blockquote>
<p>CSS &#x5C31;&#x5B8C;&#x5168;&#x5305;&#x542B;&#x5728;&#x5408;&#x5E76;&#x7684;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x518D;&#x4E5F;&#x4E0D;&#x9700;&#x8981;&#x91CD;&#x65B0;&#x4E0B;&#x8F7D;&#x3002;</p>
</blockquote>
<p>2&#x3001;&#x61D2;&#x52A0;&#x8F7D;&#xFF08;&#x63A8;&#x8350;&#xFF09;</p>
<p>&#x5982;&#x679C;&#x60F3;&#x53D1;&#x6325;&#x5E94;&#x7528;&#x4E2D;&#x591A;&#x91CD;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x7684;&#x4F18;&#x52BF;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x6BCF;&#x4E2A;&#x5165;&#x53E3;&#x70B9;&#x5305;&#x542B;&#x5404;&#x81EA;&#x7684; CSS&#x3002;</p>
<blockquote>
<p>&#x628A;&#x6A21;&#x5757;&#x7528;&#x6587;&#x4EF6;&#x5939;&#x5206;&#x79BB;&#xFF0C;&#x6BCF;&#x4E2A;&#x6587;&#x4EF6;&#x5939;&#x6709;&#x5404;&#x81EA;&#x7684; CSS &#x548C; JavaScript &#x6587;&#x4EF6;&#x3002;<br>&#x518D;&#x6B21;&#xFF0C;&#x5F53;&#x5E94;&#x7528;&#x53D1;&#x5E03;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5BFC;&#x5165;&#x7684; CSS &#x5DF2;&#x7ECF;&#x52A0;&#x8F7D;&#x5230;&#x6BCF;&#x4E2A;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x4E2D;&#x3002;</p>
</blockquote>
<p>3&#x3001;&#x5B9A;&#x5236;&#x7EC4;&#x4EF6;css</p>
<p>&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x8FD9;&#x4E2A;&#x7B56;&#x7565;&#x4E3A;&#x6BCF;&#x4E2A;&#x7EC4;&#x4EF6;&#x521B;&#x5EFA; CSS &#x6587;&#x4EF6;&#xFF0C;&#x53EF;&#x4EE5;&#x8BA9;&#x7EC4;&#x4EF6;&#x540D;&#x548C; CSS &#x4E2D;&#x7684; class &#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x6765;&#x907F;&#x514D;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;&#x4E2D;&#x7684;&#x4E00;&#x4E9B; class &#x5E72;&#x6270;&#x5230;&#x53E6;&#x5916;&#x4E00;&#x4E9B;&#x7EC4;&#x4EF6;&#x7684; class&#x3002;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;</p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack008.png" alt="&#x5B9A;&#x5236;&#x7EC4;&#x4EF6;css"></p>
<p>4&#x3001;&#x4F7F;&#x7528;&#x5185;&#x8054;&#x6837;&#x5F0F;&#x53D6;&#x4EE3; CSS &#x6587;&#x4EF6;</p>
<p>&#x5728; &#x201C;React Native&#x201D; &#x4E2D;&#x4E0D;&#x518D;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x4EFB;&#x4F55; CSS &#x6587;&#x4EF6;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x4F7F;&#x7528; style &#x5C5E;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x628A;&#x4F60;&#x7684; CSS &#x5B9A;&#x4E49;&#x6210;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x90A3;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x9879;&#x76EE;&#x91CD;&#x65B0;&#x6765;&#x8003;&#x7565;&#x4F60;&#x7684; CSS &#x7B56;&#x7565;&#x3002;</p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack009.png" alt="&#x4F7F;&#x7528;&#x5185;&#x8054;&#x6837;&#x5F0F;&#x53D6;&#x4EE3; CSS &#x6587;&#x4EF6;"></p>
<h3 id="&#x52A0;&#x8F7D;sass"><a href="#&#x52A0;&#x8F7D;sass" class="headerlink" title="&#x52A0;&#x8F7D;sass"></a>&#x52A0;&#x8F7D;sass</h3><p>&#x4E0B;&#x8F7D;&#x4F9D;&#x8D56;</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install sass-loader -save-dev</div></pre></td></tr></table></figure>
<p>&#x4FEE;&#x6539;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x53EF;&#x4EE5;&#x5728; js &#x4E2D;&#x5F15;&#x7528; sass &#x7684;&#x52A0;&#x8F7D;&#x5668;</span></div><div class="line">{</div><div class="line">    <span class="attr">test</span>: <span class="regexp">/\.scss$/</span>,</div><div class="line">    <span class="attr">loader</span>: <span class="string">&apos;style!css!sass&apos;</span></div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x5B89;&#x88C5;sass-loader&#x4E4B;&#x540E;&#x8FD0;&#x884C;&#x8FD0;&#x884C; <code>npm run develop</code> &#x65F6;&#x62A5;&#x9519;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack010.png" alt="&#x5B89;&#x88C5;sass-loader&#x4E4B;&#x540E;&#x8FD0;&#x884C;&#x62A5;&#x9519;"></p>
<p>&#x89E3;&#x51B3;&#xFF1A;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install node-sass -save-dev</div></pre></td></tr></table></figure>
<h3 id="&#x56FE;&#x7247;&#x5904;&#x7406;"><a href="#&#x56FE;&#x7247;&#x5904;&#x7406;" class="headerlink" title="&#x56FE;&#x7247;&#x5904;&#x7406;"></a>&#x56FE;&#x7247;&#x5904;&#x7406;</h3><blockquote>
<p>&#x76F4;&#x5230; HTTP/2 &#x624D;&#x80FD;&#x5728;&#x5E94;&#x7528;&#x52A0;&#x8F7D;&#x7684;&#x65F6;&#x5019;&#x907F;&#x514D;&#x8BBE;&#x7F6E;&#x592A;&#x591A; HTTP &#x8BF7;&#x6C42;&#x3002;<br>&#x6839;&#x636E;&#x6D4F;&#x89C8;&#x5668;&#x4E0D;&#x540C;&#x5FC5;&#x987B;&#x8BBE;&#x7F6E;&#x5E76;&#x884C;&#x8BF7;&#x6C42;&#x6570;&#xFF0C;&#x5982;&#x679C;&#x5728; CSS &#x4E2D;&#x52A0;&#x8F7D;&#x4E86;&#x592A;&#x591A;&#x56FE;&#x7247;&#x7684;&#x8BDD;&#xFF0C;&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x628A;&#x8FD9;&#x4E9B;&#x56FE;&#x7247;&#x8F6C;&#x6210; BASE64 &#x5B57;&#x7B26;&#x4E32;&#x7136;&#x540E;&#x5185;&#x8054;&#x5230; CSS &#x91CC;&#x6765;&#x964D;&#x4F4E;&#x5FC5;&#x8981;&#x7684;&#x8BF7;&#x6C42;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x53D6;&#x51B3;&#x4E8E;&#x56FE;&#x7247;&#x5927;&#x5C0F;&#x3002;<br>&#x9700;&#x8981;&#x4E3A;&#x5E94;&#x7528;&#x5E73;&#x8861;&#x4E0B;&#x8F7D;&#x7684;&#x5927;&#x5C0F;&#x548C;&#x4E0B;&#x8F7D;&#x7684;&#x6570;&#x91CF;&#xFF0C;&#x4E0D;&#x8FC7; Webpack &#x53EF;&#x4EE5;&#x8BA9;&#x8FD9;&#x4E2A;&#x5E73;&#x8861;&#x5341;&#x5206;&#x8F7B;&#x677E;&#x9002;&#x5E94;&#x3002;</p>
</blockquote>
<p>&#x4E0B;&#x8F7D;&#x8F7D;&#x4F9D;&#x8D56;</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install url-loader  file-loader --save-dev</div></pre></td></tr></table></figure>
<p>&#x4FEE;&#x6539;&#x914D;&#x7F6E;&#x6587;&#x4EF6;:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">{</div><div class="line">    <span class="attr">test</span>: <span class="regexp">/\.(png|jpg|gif|jpeg)$/</span>,</div><div class="line">    <span class="attr">loader</span>: <span class="string">&apos;url?limit=25000&apos;</span></div><div class="line">},</div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x5B57;&#x4F53;</span></div><div class="line">{</div><div class="line">    <span class="attr">test</span>: <span class="regexp">/\.(eot|woff|ttf|woff2|svg)$/</span>,</div><div class="line">    <span class="attr">loader</span>: <span class="string">&apos;url?limit=25000&apos;</span></div><div class="line">}</div></pre></td></tr></table></figure>
<blockquote>
<p>&#x52A0;&#x8F7D;&#x5668;&#x4F1A;&#x628A;&#x9700;&#x8981;&#x8F6C;&#x6362;&#x7684;&#x8DEF;&#x5F84;&#x53D8;&#x6210; BASE64 &#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5728;&#x5176;&#x4ED6;&#x7684; webpack &#x4E66;&#x4E2D;&#x63D0;&#x5230;&#x7684;&#x8FD9;&#x65B9;&#x9762;&#x4F1A;&#x628A; CSS &#x4E2D;&#x7684; &#x201C;url()&#x201D; &#x50CF;&#x5176;&#x4ED6; require &#x6216;&#x8005; import &#x6765;&#x5904;&#x7406;&#x3002;<br>&#x610F;&#x5473;&#x7740;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5B83;&#x6765;&#x5904;&#x7406;&#x6211;&#x4EEC;&#x7684;&#x56FE;&#x7247;&#x6587;&#x4EF6;&#x3002;<br>url-loader &#x4F20;&#x5165;&#x7684; limit &#x53C2;&#x6570;&#x662F;&#x544A;&#x8BC9;&#x5B83;&#x56FE;&#x7247;&#x5982;&#x679C;&#x4E0D;&#x5927;&#x4E8E; 25KB &#x7684;&#x8BDD;&#x8981;&#x81EA;&#x52A8;&#x5728;&#x5B83;&#x4ECE;&#x5C5E;&#x7684; css &#x6587;&#x4EF6;&#x4E2D;&#x8F6C;&#x6210; BASE64 &#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
</blockquote>
<p>&#x5927;&#x56FE;&#x7247;&#x5904;&#x7406;</p>
<p>&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x662F;&#x4E00;&#x4E0B;&#x60C5;&#x51B5;&#xFF1A;</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">div.img {</div><div class="line">    background: url(../image/xxx.jpg)</div><div class="line">}</div><div class="line">//&#x6216;&#x8005;</div><div class="line">var img = document.createElement(&quot;img&quot;);</div><div class="line">img.src = require(&quot;../image/xxx.jpg&quot;);</div><div class="line">document.body.appendChild(img);</div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x914D;&#x7F6E;</span></div><div class="line"><span class="built_in">module</span>: {</div><div class="line">    {</div><div class="line">      <span class="attr">test</span>: <span class="regexp">/\.(png|jpg)$/</span>,</div><div class="line">      <span class="attr">loader</span>: <span class="string">&apos;url-loader?limit=10000&amp;name=build/[name].[ext]&apos;</span></div><div class="line">    }]</div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x9488;&#x5BF9;&#x4E0A;&#x9762;&#x7684;&#x4E24;&#x79CD;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#xFF0C;loader &#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x8BC6;&#x522B;&#x5E76;&#x5904;&#x7406;&#x3002;&#x6839;&#x636E; loader &#x4E2D;&#x7684;&#x8BBE;&#x7F6E;&#xFF0C;webpack &#x4F1A;&#x5C06;&#x5C0F;&#x4E8E;&#x6307;&#x70B9;&#x5927;&#x5C0F;&#x7684;&#x6587;&#x4EF6;&#x8F6C;&#x5316;&#x6210; base64 &#x683C;&#x5F0F;&#x7684; dataUrl&#xFF0C;&#x5176;&#x4ED6;&#x56FE;&#x7247;&#x4F1A;&#x505A;&#x9002;&#x5F53;&#x7684;&#x538B;&#x7F29;&#x5E76;&#x5B58;&#x653E;&#x5728;&#x6307;&#x5B9A;&#x76EE;&#x5F55;&#x4E2D;&#x3002;</p>
<p>&#x8FD9;&#x4E00;&#x6B65;&#x7684;&#x76EE;&#x5F55;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack011.png" alt="&#x76EE;&#x5F55;&#x7ED3;&#x6784;"></p>
<h3 id="components-Hello-js"><a href="#components-Hello-js" class="headerlink" title="components/Hello.js"></a>components/Hello.js</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">import &apos;./Hello.css&apos;;</div><div class="line">import &apos;./Hello.scss&apos;;</div><div class="line">import React, {Component} from &apos;react&apos;;</div><div class="line">// &#x76F4;&#x63A5;&#x5728;js&#x4E2D;&#x5B9A;&#x4E49;&#x6837;&#x5F0F;&#xFF0C;&#x5185;&#x5D4C;&#x6837;&#x5F0F;</div><div class="line">let style = {</div><div class="line">    backgroundColor: &apos;blue&apos;</div><div class="line">};</div><div class="line">export default class Hello extends Component {</div><div class="line">    render() {</div><div class="line">        return (</div><div class="line">            &lt;div&gt;</div><div class="line">                {/*&#x5185;&#x5D4C;&#x6837;&#x5F0F;&#x7684;&#x5199;&#x6CD5;*/}</div><div class="line">                &lt;h1 style={style} onClick={()=&gt;this.myClick(&quot;fdfdf&quot;,23)}&gt;&#x4F7F;&#x7528;&#x6837;&#x5F0F;&#x6F14;&#x793A;&lt;/h1&gt;</div><div class="line">                &lt;br/&gt;</div><div class="line">                &lt;img/&gt;</div><div class="line">            &lt;/div&gt;</div><div class="line">        )</div><div class="line">    }</div><div class="line">}</div></pre></td></tr></table></figure>
<h3 id="components-base-scss"><a href="#components-base-scss" class="headerlink" title="components/_base.scss"></a>components/_base.scss</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">// &#x65B0;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;</div><div class="line">$fontSize: 100px;</div></pre></td></tr></table></figure>
<h3 id="components-Hello-css"><a href="#components-Hello-css" class="headerlink" title="components/Hello.css"></a>components/Hello.css</h3><figure class="highlight css"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="selector-tag">h1</span> {</div><div class="line">    <span class="attribute">color</span>: red;</div><div class="line">}</div><div class="line"><span class="selector-tag">img</span>{</div><div class="line">    <span class="attribute">height</span>: <span class="number">200px</span>;</div><div class="line">    <span class="attribute">width</span>: <span class="number">200px</span>;</div><div class="line">    <span class="attribute">background-image</span>: <span class="built_in">url</span>(<span class="string">&quot;../images/mv.png&quot;</span>);</div><div class="line">    <span class="attribute">-moz-background-image</span>: <span class="built_in">url</span>(<span class="string">&quot;../images/mv.png&quot;</span>);</div><div class="line">    <span class="attribute">background-size</span>: cover;</div><div class="line">}</div></pre></td></tr></table></figure>
<h3 id="components-Hello-scss"><a href="#components-Hello-scss" class="headerlink" title="components/Hello.scss"></a>components/Hello.scss</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">@import &quot;base&quot;;</div><div class="line">h1{ // &#x5229;&#x7528;&#x9759;&#x9ED8;&#x6CE8;&#x91CA;&#x63CF;&#x8FF0;&#x4E00;&#x4E2A;&#x6837;&#x5F0F; &#x8FD9;&#x79CD;&#x6CE8;&#x91CA;&#x5185;&#x5BB9;&#x4E0D;&#x4F1A;&#x51FA;&#x73B0;&#x5728;&#x751F;&#x6210;&#x7684;css&#x6587;&#x4EF6;&#x4E2D;</div><div class="line">  font-size: $fontSize; /* _base.sass&#x662F;&#x90E8;&#x5206;&#x6587;&#x4EF6;&#xFF0C;&#x4E0D;&#x4F1A;&#x751F;&#x6210;&#x5355;&#x72EC;&#x7684;css &#x8FD9;&#x79CD;&#x6CE8;&#x91CA;&#x5185;&#x5BB9;&#x4F1A;&#x51FA;&#x73B0;&#x5728;&#x751F;&#x6210;&#x7684;css&#x6587;&#x4EF6;&#x4E2D; */</div><div class="line">}</div></pre></td></tr></table></figure>
<h3 id="&#x6700;&#x7EC8;&#x7684;-webpack-develop-config-js-&#x6587;&#x4EF6;"><a href="#&#x6700;&#x7EC8;&#x7684;-webpack-develop-config-js-&#x6587;&#x4EF6;" class="headerlink" title="&#x6700;&#x7EC8;&#x7684; webpack.develop.config.js &#x6587;&#x4EF6;"></a>&#x6700;&#x7EC8;&#x7684; webpack.develop.config.js &#x6587;&#x4EF6;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// webpack &#x7684;&#x5F00;&#x53D1;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</span></div><div class="line"><span class="comment">// &#x7F16;&#x5199;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x8981;&#x6709;&#x6700;&#x57FA;&#x672C;&#x7684;&#x6587;&#x4EF6;&#x5165;&#x53E3;&#x548C;&#x8F93;&#x51FA;&#x6587;&#x4EF6;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x7B49;</span></div><div class="line"><span class="comment">// &#x91CC;&#x9762;&#x8FD8;&#x53EF;&#x4EE5;&#x52A0;loader&#x548C;&#x5404;&#x79CD;&#x63D2;&#x4EF6;&#x914D;&#x7F6E;&#x4F7F;&#x7528;</span></div><div class="line"><span class="keyword">var</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"><span class="comment">// &#x81EA;&#x52A8;&#x6253;&#x5F00;&#x6D4F;&#x89C8;&#x5668;&#x63D2;&#x4EF6;</span></div><div class="line"><span class="keyword">var</span> OpenBrowserPlugin = <span class="built_in">require</span>(<span class="string">&apos;open-browser-webpack-plugin&apos;</span>);</div><div class="line"><span class="built_in">module</span>.exports = {</div><div class="line">    <span class="comment">// &#x5355;&#x9875;&#x9762; SPA &#x7684;&#x5165;&#x53E3;&#x6587;&#x4EF6;</span></div><div class="line">    entry:[</div><div class="line">        <span class="comment">// &#x5B9E;&#x73B0;&#x6D4F;&#x89C8;&#x5668;&#x81EA;&#x52A8;&#x5237;&#x65B0;</span></div><div class="line">        <span class="string">&apos;webpack/hot/dev-server&apos;</span>,</div><div class="line">        <span class="string">&apos;webpack-dev-server/client?http://localhost:8080&apos;</span>,</div><div class="line">        path.resolve(__dirname,<span class="string">&apos;src/js/app.js&apos;</span>)</div><div class="line">    ],</div><div class="line">    <span class="comment">// &#x6784;&#x5EFA;&#x4E4B;&#x540E;&#x7684;&#x6587;&#x4EF6;&#x8F93;&#x51FA;&#x4F4D;&#x7F6E;&#x914D;&#x7F6E;</span></div><div class="line">    output: {</div><div class="line">        <span class="attr">path</span>: path.resolve(__dirname, <span class="string">&apos;dist&apos;</span>),</div><div class="line">        <span class="attr">filename</span>: <span class="string">&apos;bundle.js&apos;</span></div><div class="line">    },</div><div class="line">    <span class="attr">module</span>: {</div><div class="line">        <span class="comment">// &#x4E0D;&#x8981;&#x8F7B;&#x6613;&#x4F7F;&#x7528; noParse</span></div><div class="line">        <span class="comment">// noParse: [/moment-with-locales/],</span></div><div class="line">        <span class="comment">// preLoaders: [</span></div><div class="line">        <span class="comment">//     { test: /\.js$/, loader: &quot;eslint-loader&quot;, exclude: /node_modules/ }</span></div><div class="line">        <span class="comment">// ],</span></div><div class="line">        loaders: [</div><div class="line">            <span class="comment">// JXS &#x548C; ES6 &#x8BED;&#x6CD5;&#x8F6C;&#x6362;&#x4E3A; ES5</span></div><div class="line">            {</div><div class="line">                <span class="attr">test</span>: <span class="regexp">/\.jsx?$/</span>, <span class="comment">// &#x7528;&#x6B63;&#x5219;&#x6765;&#x5339;&#x914D;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#xFF0C;&#x8FD9;&#x6BB5;&#x610F;&#x601D;&#x662F;&#x5339;&#x914D; js &#x6216;&#x8005; jsx</span></div><div class="line">                loader: <span class="string">&apos;babel&apos;</span>, <span class="comment">// &#x52A0;&#x8F7D;&#x6A21;&#x5757; &quot;babel&quot; &#x662F; &quot;babel-loader&quot; &#x7684;&#x7F29;&#x5199;</span></div><div class="line">                query: {</div><div class="line">                    <span class="attr">presets</span>: [<span class="string">&apos;es2015&apos;</span>, <span class="string">&apos;react&apos;</span>, <span class="string">&apos;stage-0&apos;</span>, <span class="string">&apos;stage-1&apos;</span>, <span class="string">&apos;stage-2&apos;</span>, <span class="string">&apos;stage-3&apos;</span>]</div><div class="line">                }</div><div class="line">            },</div><div class="line">            <span class="comment">// &#x53EF;&#x4EE5;&#x5728; js &#x4E2D;&#x5F15;&#x7528; css &#x7684;&#x52A0;&#x8F7D;&#x5668;</span></div><div class="line">            {</div><div class="line">                <span class="attr">test</span>: <span class="regexp">/\.css$/</span>,</div><div class="line">                <span class="attr">loader</span>: <span class="string">&apos;style!css&apos;</span> <span class="comment">// &#x5982;&#x679C;&#x540C;&#x65F6;&#x4F7F;&#x7528;&#x591A;&#x4E2A;&#x52A0;&#x8F7D;&#x5668;&#xFF0C;&#x4E2D;&#x95F4;&#x7528; ! &#x8FDE;&#x63A5;&#xFF0C;&#x52A0;&#x8F7D;&#x5668;&#x7684;&#x6267;&#x884C;&#x987A;&#x5E8F;&#x662F;&#x4ECE;&#x53F3;&#x5411;&#x5DE6;</span></div><div class="line">            },</div><div class="line">            <span class="comment">// &#x53EF;&#x4EE5;&#x5728; js &#x4E2D;&#x5F15;&#x7528; sass &#x7684;&#x52A0;&#x8F7D;&#x5668;</span></div><div class="line">            {</div><div class="line">                <span class="attr">test</span>: <span class="regexp">/\.scss$/</span>,</div><div class="line">                <span class="attr">loader</span>: <span class="string">&apos;style!css!sass&apos;</span></div><div class="line">            },</div><div class="line">            <span class="comment">// &#x5904;&#x7406;&#x56FE;&#x7247;</span></div><div class="line">            {</div><div class="line">                <span class="attr">test</span>: <span class="regexp">/\.(png|jpg|gif|jpeg)$/</span>,</div><div class="line">                <span class="attr">loader</span>: <span class="string">&apos;url?limit=25000&apos;</span></div><div class="line">            },</div><div class="line">            <span class="comment">// &#x5904;&#x7406;&#x5B57;&#x4F53;</span></div><div class="line">            {</div><div class="line">                <span class="attr">test</span>: <span class="regexp">/\.(eot|woff|ttf|woff2|svg)$/</span>,</div><div class="line">                <span class="attr">loader</span>: <span class="string">&apos;url?limit=1000000&apos;</span></div><div class="line">            }</div><div class="line">        ]</div><div class="line">    },</div><div class="line">    <span class="attr">resolve</span>: {</div><div class="line">        <span class="comment">//&#x67E5;&#x627E;module&#x7684;&#x8BDD;&#x4ECE;&#x8FD9;&#x91CC;&#x5F00;&#x59CB;&#x67E5;&#x627E;</span></div><div class="line">        <span class="comment">// root: &apos;/pomy/github/flux-example/src&apos;, //&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</span></div><div class="line">        <span class="comment">//&#x81EA;&#x52A8;&#x6269;&#x5C55;&#x6587;&#x4EF6;&#x540E;&#x7F00;&#x540D;&#xFF0C;&#x610F;&#x5473;&#x7740;&#x6211;&#x4EEC;require&#x6A21;&#x5757;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x4E0D;&#x5199;&#x540E;&#x7F00;&#x540D;</span></div><div class="line">        <span class="comment">//&#x6CE8;&#x610F;&#x4E00;&#x4E0B;, extensions &#x7B2C;&#x4E00;&#x4E2A;&#x662F;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;! &#x5BF9;&#x5E94;&#x4E0D;&#x9700;&#x8981;&#x540E;&#x7F00;&#x7684;&#x60C5;&#x51B5;.</span></div><div class="line">        extensions: [<span class="string">&apos;&apos;</span>, <span class="string">&apos;.js&apos;</span>, <span class="string">&apos;.json&apos;</span>, <span class="string">&apos;.sass&apos;</span>, <span class="string">&apos;jsx&apos;</span>],</div><div class="line"></div><div class="line">        <span class="comment">//&#x6A21;&#x5757;&#x522B;&#x540D;&#x5B9A;&#x4E49;&#xFF0C;&#x65B9;&#x4FBF;&#x540E;&#x7EED;&#x76F4;&#x63A5;&#x5F15;&#x7528;&#x522B;&#x540D;&#xFF0C;&#x65E0;&#x987B;&#x591A;&#x5199;&#x957F;&#x957F;&#x7684;&#x5730;&#x5740;</span></div><div class="line">        alias: {</div><div class="line">            <span class="comment">// AppStore : &apos;js/stores/AppStores.js&apos;, //&#x540E;&#x7EED;&#x76F4;&#x63A5; require(&apos;AppStore&apos;) &#x5373;&#x53EF;</span></div><div class="line">            <span class="comment">// ActionType : &apos;js/actions/ActionType.js&apos;,</span></div><div class="line">            <span class="comment">// AppAction : &apos;js/actions/AppAction.js&apos;</span></div><div class="line">        }</div><div class="line">    },</div><div class="line">    <span class="comment">// &#x914D;&#x7F6E;&#x4E86;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x4E4B;&#x540E; react &#x548C; react-dom &#x8FD9;&#x4E9B;&#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x5305;&#x90FD;&#x4E0D;&#x4F1A;&#x88AB;&#x6784;&#x5EFA;&#x8FDB; js &#x4E2D;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x5C31;&#x9700;&#x8981;&#x901A;&#x8FC7; cdn &#x8FDB;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x5F15;&#x7528;&#x4E86;</span></div><div class="line">    <span class="comment">// &#x524D;&#x8FB9;&#x7684;&#x8FD9;&#x4E2A;&#x540D;&#x79F0;&#x662F;&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x5F15;&#x7528;&#x7528;&#x7684;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E; import React from &apos;react1&apos; &#x4E2D;&#x7684; react</span></div><div class="line">    externals: {</div><div class="line">        <span class="comment">// &apos;react1&apos;: &apos;react&apos;,</span></div><div class="line">        <span class="comment">// &apos;react-dom1&apos;: &apos;react-dom&apos;,</span></div><div class="line">        <span class="comment">// &apos;$1&apos;: &apos;jQuery&apos;</span></div><div class="line">    },</div><div class="line">    <span class="attr">plugins</span>: [</div><div class="line">        <span class="keyword">new</span> OpenBrowserPlugin({<span class="attr">url</span>: <span class="string">&apos;http://localhost:8080/&apos;</span>, <span class="attr">browser</span>: <span class="string">&apos;chrome&apos;</span>})</div><div class="line">    ]</div><div class="line">};</div></pre></td></tr></table></figure>
<hr>
<h2 id="webpack-&#x7684;&#x90E8;&#x7F72;&#x7B56;&#x7565;"><a href="#webpack-&#x7684;&#x90E8;&#x7F72;&#x7B56;&#x7565;" class="headerlink" title="webpack &#x7684;&#x90E8;&#x7F72;&#x7B56;&#x7565;"></a>webpack &#x7684;&#x90E8;&#x7F72;&#x7B56;&#x7565;</h2><h3 id="&#x4FEE;&#x6539;-npm-&#x7684;-package-json-&#x6587;&#x4EF6;"><a href="#&#x4FEE;&#x6539;-npm-&#x7684;-package-json-&#x6587;&#x4EF6;" class="headerlink" title="&#x4FEE;&#x6539; npm &#x7684; package.json &#x6587;&#x4EF6;"></a>&#x4FEE;&#x6539; npm &#x7684; package.json &#x6587;&#x4EF6;</h3><p>&#x201C;publish&#x201D;: &#x201C; webpack &#x2013;config webpack.publish.config.js  -p&#x201D;,</p>
<p>&#x6307;&#x5411;&#x751F;&#x4EA7;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x5E76;&#x4E14;&#x52A0;&#x4E0A;&#x4E86;webpack&#x7684;cli&#x7684;-p,&#x4ED6;&#x4F1A;&#x81EA;&#x52A8;&#x505A;&#x4E00;&#x4E9B;&#x4F18;&#x5316;</p>
<h3 id="&#x5206;&#x79BB;&#x5E94;&#x7528;&#x548C;&#x7B2C;&#x4E09;&#x65B9;"><a href="#&#x5206;&#x79BB;&#x5E94;&#x7528;&#x548C;&#x7B2C;&#x4E09;&#x65B9;" class="headerlink" title="&#x5206;&#x79BB;&#x5E94;&#x7528;&#x548C;&#x7B2C;&#x4E09;&#x65B9;"></a>&#x5206;&#x79BB;&#x5E94;&#x7528;&#x548C;&#x7B2C;&#x4E09;&#x65B9;</h3><p>&#x4F55;&#x65F6;&#x5E94;&#x8BE5;&#x5206;&#x79BB;&#xFF1F;</p>
<p>&#x5F53;&#x5E94;&#x7528;&#x4F9D;&#x8D56;&#x5176;&#x4ED6;&#x5E93;&#x5C24;&#x5176;&#x662F;&#x50CF; React JS &#x8FD9;&#x79CD;&#x5927;&#x578B;&#x5E93;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9700;&#x8981;&#x8003;&#x8651;&#x628A;&#x8FD9;&#x4E9B;&#x4F9D;&#x8D56;&#x5206;&#x79BB;&#x51FA;&#x53BB;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x80FD;&#x591F;&#x8BA9;&#x7528;&#x6237;&#x5728;&#x66F4;&#x65B0;&#x5E94;&#x7528;&#x4E4B;&#x540E;&#x4E0D;&#x9700;&#x8981;&#x518D;&#x6B21;&#x4E0B;&#x8F7D;&#x7B2C;&#x4E09;&#x65B9;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x5F53;&#x6EE1;&#x8DB3;&#x4E0B;&#x9762;&#x51E0;&#x4E2A;&#x60C5;&#x51B5;&#x7684;&#x65F6;&#x5019;&#x4F60;&#x5C31;&#x9700;&#x8981;&#x8FD9;&#x4E48;&#x505A;&#x4E86;&#xFF1A;</p>
<p>1&#x3001;&#x5F53;&#x4F60;&#x7684;&#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x4F53;&#x79EF;&#x8FBE;&#x5230;&#x6574;&#x4E2A;&#x5E94;&#x7528;&#x7684; 20% &#x6216;&#x8005;&#x66F4;&#x9AD8;&#x7684;&#x65F6;&#x5019;&#x3002;<br>2&#x3001;&#x66F4;&#x65B0;&#x5E94;&#x7528;&#x7684;&#x65F6;&#x5019;&#x53EA;&#x4F1A;&#x66F4;&#x65B0;&#x5F88;&#x5C0F;&#x7684;&#x4E00;&#x90E8;&#x5206;<br>3&#x3001;&#x4F60;&#x6CA1;&#x6709;&#x90A3;&#x4E48;&#x5173;&#x6CE8;&#x521D;&#x59CB;&#x52A0;&#x8F7D;&#x65F6;&#x95F4;&#xFF0C;&#x4E0D;&#x8FC7;&#x5173;&#x6CE8;&#x4F18;&#x5316;&#x90A3;&#x4E9B;&#x56DE;&#x8BBF;&#x7528;&#x6237;&#x5728;&#x4F60;&#x66F4;&#x65B0;&#x5E94;&#x7528;&#x4E4B;&#x540E;&#x7684;&#x4F53;&#x9A8C;&#x3002;<br>4&#x3001;&#x6709;&#x624B;&#x673A;&#x7528;&#x6237;&#x3002;</p>
<p>&#x4FEE;&#x6539; webpack.publish.config.js &#x6587;&#x4EF6;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"><span class="keyword">var</span> node_modules = path.resolve(__dirname, <span class="string">&apos;node_modules&apos;</span>);</div><div class="line"><span class="built_in">module</span>.exports = {</div><div class="line">    <span class="attr">entry</span>: path.resolve(__dirname,<span class="string">&apos;src/js/app.js&apos;</span>),</div><div class="line">    <span class="attr">output</span>: {</div><div class="line">        <span class="attr">path</span>: path.resolve(__dirname, <span class="string">&apos;build&apos;</span>),</div><div class="line">        <span class="attr">filename</span>: <span class="string">&apos;bundle.js&apos;</span>,</div><div class="line">    },</div><div class="line">    <span class="comment">// ...</span></div><div class="line">    plugins: [</div><div class="line">        <span class="keyword">new</span> CleanPlugin([<span class="string">&apos;dist&apos;</span>]),</div><div class="line">        <span class="comment">// &#x5206;&#x79BB;&#x7B2C;&#x4E09;&#x65B9;&#x5E94;&#x7528;&#x63D2;&#x4EF6;,name&#x5C5E;&#x6027;&#x4F1A;&#x81EA;&#x52A8;&#x6307;&#x5411;entry&#x4E2D;vendros&#x5C5E;&#x6027;&#xFF0C;filename&#x5C5E;&#x6027;&#x4E2D;&#x7684;&#x6587;&#x4EF6;&#x4F1A;&#x81EA;&#x52A8;&#x6784;&#x5EFA;&#x5230;output&#x4E2D;&#x7684;path&#x5C5E;&#x6027;&#x4E0B;&#x9762;</span></div><div class="line">        <span class="keyword">new</span> webpack.optimize.CommonsChunkPlugin({<span class="attr">name</span>: <span class="string">&apos;vendors&apos;</span>, <span class="attr">filename</span>: <span class="string">&apos;vendors.js&apos;</span>}),</div><div class="line">        <span class="comment">// &#x53EF;&#x4EE5;&#x65B0;&#x5EFA;&#x591A;&#x4E2A;&#x62BD;&#x79BB;&#x6837;&#x5F0F;&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x6709;&#x591A;&#x4E2A;css&#x6587;&#x4EF6;&#x4E86;&#x3002;</span></div><div class="line">        <span class="keyword">new</span> ExtractTextPlugin(<span class="string">&quot;app.css&quot;</span>),</div><div class="line">        <span class="keyword">new</span> webpack.DefinePlugin({</div><div class="line">            <span class="comment">//&#x53BB;&#x6389;react&#x4E2D;&#x7684;&#x8B66;&#x544A;&#xFF0C;react&#x4F1A;&#x81EA;&#x5DF1;&#x5224;&#x65AD;</span></div><div class="line">            <span class="string">&apos;process.env&apos;</span>: {</div><div class="line">                <span class="attr">NODE_ENV</span>: <span class="string">&apos;&quot;production&quot;&apos;</span></div><div class="line">            }</div><div class="line">        })</div><div class="line">    ]</div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x5176;&#x5B9E;&#x751F;&#x4EA7;&#x73AF;&#x5883;&#x7684;&#x914D;&#x7F6E;&#x548C;&#x5F00;&#x53D1;&#x7684;&#x914D;&#x7F6E;&#x6CA1;&#x6709;&#x592A;&#x5927;&#x7684;&#x4E0D;&#x540C;&#xFF0C;&#x4E3B;&#x8981;&#x662F;&#x628A;&#x4E0D;&#x9700;&#x8981;&#x7684;&#x4E1C;&#x897F;&#x7ED9;&#x53BB;&#x6389;&#x4E86;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm  run  publish</div></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>webpack &#x4F7F;&#x7528;<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;bundle.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;vendors.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x8BB0;&#x4F4F;&#x8981;&#x628A;&#x8FD9;&#x4E9B;&#x6587;&#x4EF6;&#x90FD;&#x52A0;&#x5165;&#x5230;&#x4F60;&#x7684; HTML &#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x4F46;&#x5728;&#x4E0A;&#x9762;&#x8FD9;&#x79CD;&#x5F15;&#x5165;&#x540E;&#xFF0C;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x6253;&#x5F00;&#x4E4B;&#x540E;&#x62A5;&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x9519;&#x8BEF;&#xFF0C;&#x662F;&#x56E0;&#x4E3A;&#x5F15;&#x5165;&#x987A;&#x5E8F;&#x7684;&#x95EE;&#x9898;</p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack012.png" alt="&#x62A5;&#x9519;"></p>
<p>&#x5C06;&#x4E0A;&#x9762; index.html &#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x4E24;&#x4E2A; js &#x6587;&#x4EF6;&#x5F15;&#x5165;&#x987A;&#x5E8F;&#x8C03;&#x6362;&#xFF0C;&#x5982;&#x4E0B;</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="comment">&lt;!-- ... --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;vendors.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;bundle.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<h3 id="&#x548C;-gulp-&#x7684;&#x96C6;&#x6210;"><a href="#&#x548C;-gulp-&#x7684;&#x96C6;&#x6210;" class="headerlink" title="&#x548C; gulp &#x7684;&#x96C6;&#x6210;"></a>&#x548C; gulp &#x7684;&#x96C6;&#x6210;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// gulp &#x7684;&#x4EFB;&#x52A1;&#x662F;&#x63A7;&#x5236;&#x6267;&#x884C;&#x6D41;&#x7A0B;&#xFF0C;webpack &#x7684;&#x4EFB;&#x52A1;&#x662F;&#x5904;&#x7406;&#x590D;&#x6742;&#x5F15;&#x7528;&#x7684;&#x4F9D;&#x8D56;</span></div><div class="line"><span class="keyword">var</span> gulp = <span class="built_in">require</span>(<span class="string">&apos;gulp&apos;</span>);</div><div class="line"><span class="comment">// &#x5220;&#x9664;&#x6587;&#x4EF6;&#x548C;&#x76EE;&#x5F55;</span></div><div class="line"><span class="keyword">var</span> del = <span class="built_in">require</span>(<span class="string">&apos;del&apos;</span>);</div><div class="line"><span class="comment">// &#x6309;&#x987A;&#x5E8F;&#x6267;&#x884C;</span></div><div class="line"><span class="keyword">var</span> gulpSequence = <span class="built_in">require</span>(<span class="string">&apos;gulp-sequence&apos;</span>);</div><div class="line"><span class="comment">// &#x5F15;&#x5165;webpack&#x7684;&#x672C;&#x5730;&#x6A21;&#x5757;</span></div><div class="line"><span class="keyword">var</span> webpack = <span class="built_in">require</span>(<span class="string">&quot;webpack&quot;</span>);</div><div class="line"><span class="comment">// &#x5F15;&#x5165;wbpack&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</span></div><div class="line"><span class="keyword">var</span> webpackConfig = <span class="built_in">require</span>(<span class="string">&quot;./webpack.publish.config.js&quot;</span>);</div><div class="line">gulp.task(<span class="string">&apos;default&apos;</span>, [<span class="string">&apos;sequence&apos;</span>], <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x6210;&#x529F;&quot;</span>);</div><div class="line">});</div><div class="line"><span class="comment">// &#x6D41;&#x7A0B;&#x63A7;&#x5236;</span></div><div class="line">gulp.task(<span class="string">&apos;sequence&apos;</span>, gulpSequence(<span class="string">&apos;clean&apos;</span>,<span class="string">&apos;webpack&apos;</span>));</div><div class="line"><span class="comment">// &#x5220;&#x9664;&#x6587;&#x4EF6;&#x548C;&#x6587;&#x4EF6;&#x5939;</span></div><div class="line">gulp.task(<span class="string">&apos;clean&apos;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">cb</span>) </span>{</div><div class="line">    <span class="comment">//del(&apos;dist);// &#x5982;&#x679C;&#x76F4;&#x63A5;&#x7ED9;dist&#x7684;&#x76EE;&#x5F55;&#xFF0C;&#x9879;&#x76EE;&#x542F;&#x52A8;&#x7684;&#x987A;&#x5E8F;&#x8FD8;&#x6709;&#x6E05;&#x9664;&#x7ED3;&#x679C;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x5199;&#x7684;&#x66F4;&#x8BE6;&#x7EC6;&#x4E00;&#x4E9B;</span></div><div class="line">    del([<span class="string">&apos;dist/js&apos;</span>,<span class="string">&apos;dist/css&apos;</span>,<span class="string">&apos;dist/img&apos;</span>,<span class="string">&apos;dist/*.html&apos;</span>]);</div><div class="line">    setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</div><div class="line">        <span class="keyword">return</span> cb();</div><div class="line">    },<span class="number">3000</span>)</div><div class="line"></div><div class="line">});</div><div class="line"><span class="comment">//&#x5199;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#xFF0C;&#x5728;gulp&#x4E2D;&#x6267;&#x884C;webpack&#x7684;&#x6784;&#x5EFA;</span></div><div class="line"><span class="comment">// gulp &#x8D1F;&#x8D23;&#x4EFB;&#x52A1;&#x6D41;&#x7A0B;&#x90E8;&#x5206;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;webpack &#x8D1F;&#x8D23;&#x590D;&#x6742;&#x6A21;&#x5757;&#x7CFB;&#x7EDF;&#x7684;&#x5F15;&#x7528;&#x5206;&#x79BB;&#x5DE5;&#x4F5C;</span></div><div class="line">gulp.task(<span class="string">&apos;webpack&apos;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">cb</span>) </span>{</div><div class="line">    setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</div><div class="line">        <span class="comment">// &#x6267;&#x884C;webpack&#x7684;&#x6784;&#x5EFA;&#x4EFB;&#x52A1;</span></div><div class="line">        webpack(webpackConfig, <span class="function"><span class="keyword">function</span> (<span class="params">err, stats</span>) </span>{</div><div class="line">            <span class="keyword">if</span> (err){</div><div class="line">                <span class="built_in">console</span>.log(<span class="string">&quot;&#x6784;&#x5EFA;&#x4EFB;&#x52A1;&#x5931;&#x8D25;&quot;</span>);</div><div class="line">            }<span class="keyword">else</span>{</div><div class="line">                cb();</div><div class="line">            }</div><div class="line">        });</div><div class="line">    },<span class="number">3000</span>)</div><div class="line">});</div></pre></td></tr></table></figure>
<h3 id="&#x5408;&#x5E76;&#x6210;&#x5355;&#x6587;&#x4EF6;"><a href="#&#x5408;&#x5E76;&#x6210;&#x5355;&#x6587;&#x4EF6;" class="headerlink" title="&#x5408;&#x5E76;&#x6210;&#x5355;&#x6587;&#x4EF6;"></a>&#x5408;&#x5E76;&#x6210;&#x5355;&#x6587;&#x4EF6;</h3><p>&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#x53EA;&#x6709;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x624D;&#x4F7F;&#x7528;&#x5355;&#x5165;&#x53E3;&#x6A21;&#x5F0F;&#xFF1A;</p>
<p>1&#x3001;&#x5E94;&#x7528;&#x5F88;&#x5C0F;<br>2&#x3001;&#x5F88;&#x5C11;&#x4F1A;&#x66F4;&#x65B0;&#x5E94;&#x7528;<br>3&#x3001;&#x4F60;&#x4E0D;&#x592A;&#x5173;&#x5FC3;&#x521D;&#x59CB;&#x52A0;&#x8F7D;&#x65F6;&#x95F4;</p>
<blockquote>
<p>gulp + webpack &#x6784;&#x5EFA;&#x591A;&#x9875;&#x9762;&#x524D;&#x7AEF;&#x9879;&#x76EE;</p>
</blockquote>
<p><a href="http://cnodejs.org/topic/56df76559386fbf86ddd6916" target="_blank" rel="external">http://cnodejs.org/topic/56df76559386fbf86ddd6916</a></p>
<hr>
<h2 id="&#x5E38;&#x7528;&#x63D2;&#x4EF6;"><a href="#&#x5E38;&#x7528;&#x63D2;&#x4EF6;" class="headerlink" title="&#x5E38;&#x7528;&#x63D2;&#x4EF6;"></a>&#x5E38;&#x7528;&#x63D2;&#x4EF6;</h2><h3 id="&#x538B;&#x7F29;&#x63D2;&#x4EF6;-webpack-optimize-UglifyJsPlugin-&#x8FD9;&#x4E2A;&#x63D2;&#x4EF6;&#x662F;webpack&#x81EA;&#x5E26;&#x7684;"><a href="#&#x538B;&#x7F29;&#x63D2;&#x4EF6;-webpack-optimize-UglifyJsPlugin-&#x8FD9;&#x4E2A;&#x63D2;&#x4EF6;&#x662F;webpack&#x81EA;&#x5E26;&#x7684;" class="headerlink" title="&#x538B;&#x7F29;&#x63D2;&#x4EF6; webpack.optimize.UglifyJsPlugin, &#x8FD9;&#x4E2A;&#x63D2;&#x4EF6;&#x662F;webpack&#x81EA;&#x5E26;&#x7684;."></a>&#x538B;&#x7F29;&#x63D2;&#x4EF6; webpack.optimize.UglifyJsPlugin, &#x8FD9;&#x4E2A;&#x63D2;&#x4EF6;&#x662F;webpack&#x81EA;&#x5E26;&#x7684;.</h3><p>&#x5728;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x52A0;&#x5165;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">plugins: [</div><div class="line">  <span class="keyword">new</span> webpack.optimize.UglifyJsPlugin({</div><div class="line">        <span class="attr">compress</span>: {</div><div class="line">            <span class="attr">warnings</span>: <span class="literal">false</span></div><div class="line">        }</div><div class="line">    })</div><div class="line">]</div></pre></td></tr></table></figure>
<h3 id="&#x63D0;&#x53D6;css&#x63D2;&#x4EF6;"><a href="#&#x63D0;&#x53D6;css&#x63D2;&#x4EF6;" class="headerlink" title="&#x63D0;&#x53D6;css&#x63D2;&#x4EF6;"></a>&#x63D0;&#x53D6;css&#x63D2;&#x4EF6;</h3><p>&#x5728; webpack &#x4E2D;&#x7F16;&#x5199; js &#x6587;&#x4EF6;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; require &#x7684;&#x65B9;&#x5F0F;&#x5F15;&#x5165;&#x5176;&#x4ED6;&#x7684;&#x9759;&#x6001;&#x8D44;&#x6E90;&#xFF0C;&#x53EF;&#x901A;&#x8FC7; loade r&#x5BF9;&#x6587;&#x4EF6;&#x81EA;&#x52A8;&#x89E3;&#x6790;&#x5E76;&#x6253;&#x5305;&#x6587;&#x4EF6;&#x3002;&#x901A;&#x5E38;&#x4F1A;&#x5C06; js  &#x6587;&#x4EF6;&#x6253;&#x5305;&#x5408;&#x5E76;&#xFF0C;css&#x6587;&#x4EF6;&#x4F1A;&#x5728;&#x9875;&#x9762;&#x7684;header&#x4E2D;&#x5D4C;&#x5165;style&#x7684;&#x65B9;&#x5F0F;&#x8F7D;&#x5165;&#x9875;&#x9762;&#x3002;&#x4F46;&#x5F00;&#x53D1;&#x8FC7;&#x7A0B;&#x4E2D;&#x6211;&#x4EEC;&#x5E76;&#x4E0D;&#x60F3;&#x5C06;&#x6837;&#x5F0F;&#x6253;&#x5728;&#x811A;&#x672C;&#x4E2D;&#xFF0C;&#x6700;&#x597D;&#x53EF;&#x4EE5;&#x72EC;&#x7ACB;&#x751F;&#x6210;css&#x6587; &#x4EF6;&#xFF0C;&#x4EE5;&#x5916;&#x94FE;&#x7684;&#x5F62;&#x5F0F;&#x52A0;&#x8F7D;&#x3002;&#x8FD9;&#x65F6;extract-text-webpack-plugin&#x63D2;&#x4EF6;&#x53EF;&#x4EE5;&#x5E2E;&#x6211;&#x4EEC;&#x8FBE;&#x5230;&#x60F3;&#x8981;&#x7684;&#x6548;&#x679C;&#x3002;&#x9700;&#x8981;&#x4F7F;&#x7528;npm&#x7684;&#x65B9;&#x5F0F;&#x52A0;&#x8F7D;&#x63D2;&#x4EF6;&#xFF0C;&#x7136;&#x540E; &#x53C2;&#x89C1;&#x4E0B;&#x9762;&#x7684;&#x914D;&#x7F6E;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x5C06;js&#x4E2D;&#x7684;css&#x6587;&#x4EF6;&#x63D0;&#x53D6;&#xFF0C;&#x5E76;&#x4EE5;&#x6307;&#x5B9A;&#x7684;&#x6587;&#x4EF6;&#x540D;&#x6765;&#x8FDB;&#x884C;&#x52A0;&#x8F7D;&#x3002;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install extract-text-webpack-plugin --save-dev</div></pre></td></tr></table></figure>
<p>&#x53EA;&#x80FD;&#x628A; css &#x62BD;&#x51FA;&#x6765;&#xFF0C;&#x4F46;&#x662F; sass &#x7684;&#x6837;&#x5F0F;&#x4E0D;&#x80FD;&#x5206;&#x79BB;&#x51FA;&#x6765;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> ExtractTextPlugin = <span class="built_in">require</span>(<span class="string">&quot;extract-text-webpack-plugin&quot;</span>);</div><div class="line">plugins: [</div><div class="line">  <span class="keyword">new</span> ExtractTextPlugin(<span class="string">&quot;app.css&quot;</span>)</div><div class="line">]</div></pre></td></tr></table></figure>
<h3 id="&#x81EA;&#x52A8;&#x521B;&#x5EFA;-index-Html-&#x9875;&#x9762;&#x63D2;&#x4EF6;"><a href="#&#x81EA;&#x52A8;&#x521B;&#x5EFA;-index-Html-&#x9875;&#x9762;&#x63D2;&#x4EF6;" class="headerlink" title="&#x81EA;&#x52A8;&#x521B;&#x5EFA; index.Html &#x9875;&#x9762;&#x63D2;&#x4EF6;"></a>&#x81EA;&#x52A8;&#x521B;&#x5EFA; index.Html &#x9875;&#x9762;&#x63D2;&#x4EF6;</h3><p>html-webpack-plugin</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&apos;html-webpack-plugin&apos;</span>);</div><div class="line">plugins: [</div><div class="line">  <span class="keyword">new</span> HtmlWebpackPlugin({</div><div class="line">      <span class="attr">template</span>: <span class="string">&apos;./src/template.html&apos;</span>,</div><div class="line">      <span class="attr">htmlWebpackPlugin</span>: {</div><div class="line">          <span class="string">&quot;files&quot;</span>: {</div><div class="line">              <span class="string">&quot;css&quot;</span>: [<span class="string">&quot;app.css&quot;</span>],</div><div class="line">              <span class="string">&quot;js&quot;</span>: [<span class="string">&quot;vendors.js&quot;</span>, <span class="string">&quot;bundle.js&quot;</span>]</div><div class="line">          }</div><div class="line">      },</div><div class="line">      <span class="comment">// &#x538B;&#x7F29; html &#x6587;&#x6863;</span></div><div class="line">      minify: {</div><div class="line">          <span class="attr">removeComments</span>: <span class="literal">true</span>,</div><div class="line">          <span class="attr">collapseWhitespace</span>: <span class="literal">true</span>,</div><div class="line">          <span class="attr">removeAttributeQuotes</span>: <span class="literal">true</span></div><div class="line">      }</div><div class="line">  })</div><div class="line">]</div></pre></td></tr></table></figure>
<h3 id="&#x4F18;&#x5316;&#x7B2C;&#x4E09;&#x65B9;&#x5305;"><a href="#&#x4F18;&#x5316;&#x7B2C;&#x4E09;&#x65B9;&#x5305;" class="headerlink" title="&#x4F18;&#x5316;&#x7B2C;&#x4E09;&#x65B9;&#x5305;"></a>&#x4F18;&#x5316;&#x7B2C;&#x4E09;&#x65B9;&#x5305;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">plugins: [</div><div class="line">  <span class="keyword">new</span> webpack.DefinePlugin({</div><div class="line">      <span class="comment">//&#x53BB;&#x6389;react&#x4E2D;&#x7684;&#x8B66;&#x544A;&#xFF0C;react&#x4F1A;&#x81EA;&#x5DF1;&#x5224;&#x65AD;</span></div><div class="line">      <span class="string">&apos;process.env&apos;</span>: {</div><div class="line">          <span class="attr">NODE_ENV</span>: <span class="string">&apos;&quot;production&quot;&apos;</span></div><div class="line">      }</div><div class="line">  })</div><div class="line">]</div></pre></td></tr></table></figure>
<h3 id="&#x81EA;&#x52A8;&#x6253;&#x5F00;&#x6D4F;&#x89C8;&#x5668;&#x63D2;&#x4EF6;"><a href="#&#x81EA;&#x52A8;&#x6253;&#x5F00;&#x6D4F;&#x89C8;&#x5668;&#x63D2;&#x4EF6;" class="headerlink" title="&#x81EA;&#x52A8;&#x6253;&#x5F00;&#x6D4F;&#x89C8;&#x5668;&#x63D2;&#x4EF6;"></a>&#x81EA;&#x52A8;&#x6253;&#x5F00;&#x6D4F;&#x89C8;&#x5668;&#x63D2;&#x4EF6;</h3><p>open-browser-webpack-plugin</p>
<p><a href="https://github.com/baldore/open-browser-webpack-" target="_blank" rel="external">https://github.com/baldore/open-browser-webpack-</a></p>
<blockquote>
<p>webpack.develop.config.js</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x81EA;&#x52A8;&#x6253;&#x5F00;&#x6D4F;&#x89C8;&#x5668;&#x63D2;&#x4EF6;</span></div><div class="line"><span class="keyword">var</span> OpenBrowserPlugin = <span class="built_in">require</span>(<span class="string">&apos;open-browser-webpack-plugin&apos;</span>);</div><div class="line">plugins: [</div><div class="line">    <span class="keyword">new</span> OpenBrowserPlugin({<span class="attr">url</span>: <span class="string">&apos;http://localhost:8080/&apos;</span>, <span class="attr">browser</span>: <span class="string">&apos;chrome&apos;</span>})</div><div class="line">]</div></pre></td></tr></table></figure>
<h3 id="&#x63D0;&#x53D6;-js-&#x516C;&#x5171;&#x90E8;&#x5206;&#x63D2;&#x4EF6;"><a href="#&#x63D0;&#x53D6;-js-&#x516C;&#x5171;&#x90E8;&#x5206;&#x63D2;&#x4EF6;" class="headerlink" title="&#x63D0;&#x53D6; js &#x516C;&#x5171;&#x90E8;&#x5206;&#x63D2;&#x4EF6;"></a>&#x63D0;&#x53D6; js &#x516C;&#x5171;&#x90E8;&#x5206;&#x63D2;&#x4EF6;</h3><p>&#x63D0;&#x53D6;&#x516C;&#x5171;&#x6587;&#x4EF6;: CommonsChunkPlugin</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">plugins: [</div><div class="line">    <span class="comment">// &#x5206;&#x79BB;&#x7B2C;&#x4E09;&#x65B9;&#x5E94;&#x7528;&#x63D2;&#x4EF6;,name&#x5C5E;&#x6027;&#x4F1A;&#x81EA;&#x52A8;&#x6307;&#x5411; entry &#x4E2D; vendros &#x5C5E;&#x6027;&#xFF0C;filename &#x5C5E;&#x6027;&#x4E2D;&#x7684;&#x6587;&#x4EF6;&#x4F1A;&#x81EA;&#x52A8;&#x6784;&#x5EFA;&#x5230; output &#x4E2D;&#x7684; path &#x5C5E;&#x6027;&#x4E0B;&#x9762;</span></div><div class="line">    <span class="keyword">new</span> webpack.optimize.CommonsChunkPlugin({<span class="attr">name</span>: <span class="string">&apos;vendors&apos;</span>, <span class="attr">filename</span>: <span class="string">&apos;vendors.js&apos;</span>}),</div><div class="line">]</div></pre></td></tr></table></figure>
<h3 id="ProvidePlugin&#x63D2;&#x4EF6;"><a href="#ProvidePlugin&#x63D2;&#x4EF6;" class="headerlink" title="ProvidePlugin&#x63D2;&#x4EF6;"></a>ProvidePlugin&#x63D2;&#x4EF6;</h3><p>&#x81EA;&#x52A8;&#x6DFB;&#x52A0;&#x5F15;&#x7528;&#x63D2;&#x4EF6;&#xFF0C;&#x5168;&#x5C40;&#x66B4;&#x9732;&#x63D2;&#x4EF6;&#xFF0C;&#x76F4;&#x63A5;&#x4F7F;&#x7528;</p>
<h3 id="&#x5220;&#x9664;&#x76EE;&#x5F55;&#x63D2;&#x4EF6;"><a href="#&#x5220;&#x9664;&#x76EE;&#x5F55;&#x63D2;&#x4EF6;" class="headerlink" title="&#x5220;&#x9664;&#x76EE;&#x5F55;&#x63D2;&#x4EF6;"></a>&#x5220;&#x9664;&#x76EE;&#x5F55;&#x63D2;&#x4EF6;</h3><p>clean-webpack-plugin</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> CleanPlugin = <span class="built_in">require</span>(<span class="string">&quot;clean-webpack-plugin&quot;</span>);</div><div class="line">plugins: [</div><div class="line">  <span class="keyword">new</span> CleanPlugin([<span class="string">&apos;dist&apos;</span>]),</div><div class="line">]</div></pre></td></tr></table></figure>
<h3 id="&#x62F7;&#x8D1D;&#x6587;&#x4EF6;&#x63D2;&#x4EF6;"><a href="#&#x62F7;&#x8D1D;&#x6587;&#x4EF6;&#x63D2;&#x4EF6;" class="headerlink" title="&#x62F7;&#x8D1D;&#x6587;&#x4EF6;&#x63D2;&#x4EF6;"></a>&#x62F7;&#x8D1D;&#x6587;&#x4EF6;&#x63D2;&#x4EF6;</h3><p>copy-webpack-plugin</p>
<h3 id="&#x5408;&#x5E76;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x63D2;&#x4EF6;"><a href="#&#x5408;&#x5E76;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x63D2;&#x4EF6;" class="headerlink" title="&#x5408;&#x5E76;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x63D2;&#x4EF6;"></a>&#x5408;&#x5E76;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x63D2;&#x4EF6;</h3><blockquote>
<p>webpack-config</p>
</blockquote>
<p><a href="https://github.com/mdreizin/webpack-config" target="_blank" rel="external">https://github.com/mdreizin/webpack-config</a></p>
<h3 id="&#x6700;&#x7EC8;&#x7684;-webpack-publish-config-js-&#x6587;&#x4EF6;"><a href="#&#x6700;&#x7EC8;&#x7684;-webpack-publish-config-js-&#x6587;&#x4EF6;" class="headerlink" title="&#x6700;&#x7EC8;&#x7684; webpack.publish.config.js &#x6587;&#x4EF6;"></a>&#x6700;&#x7EC8;&#x7684; webpack.publish.config.js &#x6587;&#x4EF6;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// webpack &#x7684;&#x53D1;&#x5E03;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</span></div><div class="line"><span class="keyword">var</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"><span class="keyword">var</span> webpack = <span class="built_in">require</span>(<span class="string">&apos;webpack&apos;</span>);</div><div class="line"><span class="comment">// &#x81EA;&#x52A8;&#x751F;&#x6210;index.html&#x9875;&#x9762;&#x63D2;&#x4EF6;</span></div><div class="line"><span class="keyword">var</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&apos;html-webpack-plugin&apos;</span>);</div><div class="line"><span class="comment">// &#x63D0;&#x53D6;css&#x6587;&#x4EF6;&#x7684;&#x63D2;&#x4EF6;</span></div><div class="line"><span class="keyword">var</span> ExtractTextPlugin = <span class="built_in">require</span>(<span class="string">&quot;extract-text-webpack-plugin&quot;</span>);</div><div class="line"><span class="comment">// &#x5220;&#x9664;&#x6587;&#x4EF6;&#x5939;</span></div><div class="line"><span class="keyword">var</span> CleanPlugin = <span class="built_in">require</span>(<span class="string">&apos;clean-webpack-plugin&apos;</span>);</div><div class="line"><span class="built_in">module</span>.exports = {</div><div class="line">    <span class="comment">// &#x5355;&#x9875;&#x9762; SPA &#x7684;&#x5165;&#x53E3;&#x6587;&#x4EF6;</span></div><div class="line">    entry: {</div><div class="line">        <span class="attr">app</span>: path.resolve(__dirname,<span class="string">&apos;src/js/app.js&apos;</span>),</div><div class="line">        <span class="comment">// &#x5F53; react &#x4F5C;&#x4E3A;&#x4E00;&#x4E2A; node  &#x6A21;&#x5757;&#x5B89;&#x88C5;&#x7684;&#x65F6;&#x5019;&#xFF0C;</span></div><div class="line">        <span class="comment">// &#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x6307;&#x5411;&#x5B83;&#xFF0C;&#x5C31;&#x6BD4;&#x5982; require(&apos;react&apos;);</span></div><div class="line">        vendors: [<span class="string">&apos;react&apos;</span>, <span class="string">&apos;react-dom&apos;</span>]</div><div class="line">    },</div><div class="line">    <span class="comment">// &#x6784;&#x5EFA;&#x4E4B;&#x540E;&#x7684;&#x6587;&#x4EF6;&#x8F93;&#x51FA;&#x4F4D;&#x7F6E;&#x914D;&#x7F6E;</span></div><div class="line">    output: {</div><div class="line">        <span class="attr">path</span>: path.resolve(__dirname, <span class="string">&apos;dist&apos;</span>),</div><div class="line">        <span class="attr">filename</span>: <span class="string">&apos;bundle.js&apos;</span></div><div class="line">    },</div><div class="line">    <span class="attr">module</span>: {</div><div class="line">        <span class="attr">loaders</span>: [</div><div class="line">            <span class="comment">// JXS &#x548C; ES6 &#x8BED;&#x6CD5;&#x8F6C;&#x6362;&#x4E3A; ES5</span></div><div class="line">            {</div><div class="line">                <span class="attr">test</span>: <span class="regexp">/\.jsx?$/</span>, <span class="comment">// &#x7528;&#x6B63;&#x5219;&#x6765;&#x5339;&#x914D;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#xFF0C;&#x8FD9;&#x6BB5;&#x610F;&#x601D;&#x662F;&#x5339;&#x914D; js &#x6216;&#x8005; jsx</span></div><div class="line">                loader: <span class="string">&apos;babel&apos;</span>, <span class="comment">// &#x52A0;&#x8F7D;&#x6A21;&#x5757; &quot;babel&quot; &#x662F; &quot;babel-loader&quot; &#x7684;&#x7F29;&#x5199;</span></div><div class="line">                query: {</div><div class="line">                    <span class="attr">presets</span>: [<span class="string">&apos;es2015&apos;</span>, <span class="string">&apos;react&apos;</span>, <span class="string">&apos;stage-0&apos;</span>, <span class="string">&apos;stage-1&apos;</span>, <span class="string">&apos;stage-2&apos;</span>, <span class="string">&apos;stage-3&apos;</span>]</div><div class="line">                }</div><div class="line">            },</div><div class="line">            <span class="comment">// &#x53EF;&#x4EE5;&#x5728; js &#x4E2D;&#x5F15;&#x7528; css &#x7684;&#x52A0;&#x8F7D;&#x5668;</span></div><div class="line">            {</div><div class="line">                <span class="attr">test</span>: <span class="regexp">/\.css$/</span>,</div><div class="line">                <span class="attr">loader</span>: ExtractTextPlugin.extract(<span class="string">&quot;style-loader&quot;</span>, <span class="string">&quot;css-loader&quot;</span>) <span class="comment">// &#x5982;&#x679C;&#x540C;&#x65F6;&#x4F7F;&#x7528;&#x591A;&#x4E2A;&#x52A0;&#x8F7D;&#x5668;&#xFF0C;&#x4E2D;&#x95F4;&#x7528; ! &#x8FDE;&#x63A5;&#xFF0C;&#x52A0;&#x8F7D;&#x5668;&#x7684;&#x6267;&#x884C;&#x987A;&#x5E8F;&#x662F;&#x4ECE;&#x53F3;&#x5411;&#x5DE6;</span></div><div class="line">            },</div><div class="line">            <span class="comment">// &#x53EF;&#x4EE5;&#x5728; js &#x4E2D;&#x5F15;&#x7528; sass &#x7684;&#x52A0;&#x8F7D;&#x5668;</span></div><div class="line">            {</div><div class="line">                <span class="attr">test</span>: <span class="regexp">/\.scss$/</span>,</div><div class="line">                <span class="attr">loader</span>: ExtractTextPlugin.extract(<span class="string">&quot;style-loader&quot;</span>, <span class="string">&quot;css-loader!sass-loader&quot;</span>)</div><div class="line">            },</div><div class="line">            <span class="comment">// &#x5904;&#x7406;&#x56FE;&#x7247;</span></div><div class="line">            {</div><div class="line">                <span class="attr">test</span>: <span class="regexp">/\.(png|jpg|gif|jpeg)$/</span>,</div><div class="line">                <span class="attr">loader</span>: <span class="string">&apos;url?limit=25000&amp;name=images/[name].[ext]&apos;</span></div><div class="line">            },</div><div class="line">            <span class="comment">// &#x5904;&#x7406;&#x5B57;&#x4F53;</span></div><div class="line">            {</div><div class="line">                <span class="attr">test</span>: <span class="regexp">/\.(eot|woff|ttf|woff2|svg)$/</span>,</div><div class="line">                <span class="attr">loader</span>: <span class="string">&apos;url?limit=1000000&amp;name=fonts/[name],[ext]&apos;</span></div><div class="line">            }</div><div class="line">        ]</div><div class="line">    },</div><div class="line">    <span class="attr">resolve</span>: {</div><div class="line">        <span class="comment">//&#x67E5;&#x627E;module&#x7684;&#x8BDD;&#x4ECE;&#x8FD9;&#x91CC;&#x5F00;&#x59CB;&#x67E5;&#x627E;</span></div><div class="line">        <span class="comment">// root: &apos;/pomy/github/flux-example/src&apos;, //&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</span></div><div class="line">        <span class="comment">//&#x81EA;&#x52A8;&#x6269;&#x5C55;&#x6587;&#x4EF6;&#x540E;&#x7F00;&#x540D;&#xFF0C;&#x610F;&#x5473;&#x7740;&#x6211;&#x4EEC; require &#x6A21;&#x5757;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x4E0D;&#x5199;&#x540E;&#x7F00;&#x540D;</span></div><div class="line">        <span class="comment">//&#x6CE8;&#x610F;&#x4E00;&#x4E0B;, extensions &#x7B2C;&#x4E00;&#x4E2A;&#x662F;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;! &#x5BF9;&#x5E94;&#x4E0D;&#x9700;&#x8981;&#x540E;&#x7F00;&#x7684;&#x60C5;&#x51B5;.</span></div><div class="line">        extensions: [<span class="string">&apos;&apos;</span>, <span class="string">&apos;.js&apos;</span>, <span class="string">&apos;.json&apos;</span>, <span class="string">&apos;.sass&apos;</span>, <span class="string">&apos;jsx&apos;</span>],</div><div class="line">        <span class="comment">//&#x6A21;&#x5757;&#x522B;&#x540D;&#x5B9A;&#x4E49;&#xFF0C;&#x65B9;&#x4FBF;&#x540E;&#x7EED;&#x76F4;&#x63A5;&#x5F15;&#x7528;&#x522B;&#x540D;&#xFF0C;&#x65E0;&#x987B;&#x591A;&#x5199;&#x957F;&#x957F;&#x7684;&#x5730;&#x5740;</span></div><div class="line">        <span class="comment">// alias: {</span></div><div class="line">        <span class="comment">//     AppStore : &apos;js/stores/AppStores.js&apos;,//&#x540E;&#x7EED;&#x76F4;&#x63A5; require(&apos;AppStore&apos;) &#x5373;&#x53EF;</span></div><div class="line">        <span class="comment">//         ActionType : &apos;js/actions/ActionType.js&apos;,</span></div><div class="line">        <span class="comment">//         AppAction : &apos;js/actions/AppAction.js&apos;</span></div><div class="line">        <span class="comment">// }</span></div><div class="line">    },</div><div class="line">    <span class="attr">plugins</span>: [</div><div class="line">        <span class="comment">// &#x4F7F;&#x7528;&#x4E86;&#x8BE5;&#x63D2;&#x4EF6;&#x5C31;&#x4E0D;&#x9002;&#x7528; gulp &#x4E86;</span></div><div class="line">        <span class="keyword">new</span> CleanPlugin([<span class="string">&apos;dist&apos;</span>]),</div><div class="line">        <span class="comment">// &#x5206;&#x79BB;&#x7B2C;&#x4E09;&#x65B9;&#x5E94;&#x7528;&#x63D2;&#x4EF6;,name&#x5C5E;&#x6027;&#x4F1A;&#x81EA;&#x52A8;&#x6307;&#x5411; entry &#x4E2D; vendros &#x5C5E;&#x6027;&#xFF0C;filename &#x5C5E;&#x6027;&#x4E2D;&#x7684;&#x6587;&#x4EF6;&#x4F1A;&#x81EA;&#x52A8;&#x6784;&#x5EFA;&#x5230;output&#x4E2D;&#x7684;path&#x5C5E;&#x6027;&#x4E0B;&#x9762;</span></div><div class="line">        <span class="keyword">new</span> webpack.optimize.CommonsChunkPlugin({<span class="attr">name</span>: <span class="string">&apos;vendors&apos;</span>, <span class="attr">filename</span>: <span class="string">&apos;vendors.js&apos;</span>}),</div><div class="line">        <span class="comment">// &#x7528;webpack&#x538B;&#x7F29;&#x4EE3;&#x7801;&#xFF0C;&#x53EF;&#x4EE5;&#x5FFD;&#x7565;&#x4EE3;&#x7801;&#x4E2D;&#x7684;&#x8B66;&#x544A;</span></div><div class="line">        <span class="keyword">new</span> webpack.optimize.UglifyJsPlugin({</div><div class="line">            <span class="attr">compress</span>: {</div><div class="line">                <span class="attr">warnings</span>: <span class="literal">false</span></div><div class="line">            }</div><div class="line">        }),</div><div class="line">        <span class="comment">// &#x53EF;&#x4EE5;&#x65B0;&#x5EFA;&#x591A;&#x4E2A;&#x62BD;&#x79BB;&#x6837;&#x5F0F;&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x6709;&#x591A;&#x4E2A;css&#x6587;&#x4EF6;&#x4E86;&#x3002;</span></div><div class="line">        <span class="keyword">new</span> ExtractTextPlugin(<span class="string">&quot;app.css&quot;</span>),</div><div class="line">        <span class="keyword">new</span> HtmlWebpackPlugin({</div><div class="line">            <span class="attr">template</span>: <span class="string">&apos;./src/template.html&apos;</span>,</div><div class="line">            <span class="attr">htmlWebpackPlugin</span>: {</div><div class="line">                <span class="string">&quot;files&quot;</span>: {</div><div class="line">                    <span class="string">&quot;css&quot;</span>: [<span class="string">&quot;app.css&quot;</span>],</div><div class="line">                    <span class="string">&quot;js&quot;</span>: [<span class="string">&quot;vendors.js&quot;</span>, <span class="string">&quot;bundle.js&quot;</span>]</div><div class="line">                }</div><div class="line">            },</div><div class="line">            <span class="comment">// &#x538B;&#x7F29; html &#x6587;&#x6863;</span></div><div class="line">            minify: {</div><div class="line">                <span class="attr">removeComments</span>: <span class="literal">true</span>,</div><div class="line">                <span class="attr">collapseWhitespace</span>: <span class="literal">true</span>,</div><div class="line">                <span class="attr">removeAttributeQuotes</span>: <span class="literal">true</span></div><div class="line">            }</div><div class="line">        }),</div><div class="line">        <span class="comment">// &#x4F18;&#x5316;&#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x5305;&#xFF0C;&#x51CF;&#x5C11;&#x4EE3;&#x7801;&#x91CF;</span></div><div class="line">        <span class="keyword">new</span> webpack.DefinePlugin({</div><div class="line">            <span class="comment">//&#x53BB;&#x6389;react&#x4E2D;&#x7684;&#x8B66;&#x544A;&#xFF0C;react&#x4F1A;&#x81EA;&#x5DF1;&#x5224;&#x65AD;</span></div><div class="line">            <span class="string">&apos;process.env&apos;</span>: {</div><div class="line">                <span class="attr">NODE_ENV</span>: <span class="string">&apos;&quot;production&quot;&apos;</span></div><div class="line">            }</div><div class="line">        })</div><div class="line">    ]</div><div class="line">};</div></pre></td></tr></table></figure>
<p>&#x6700;&#x7EC8;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;</p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack013.png" alt="&#x76EE;&#x5F55;&#x7ED3;&#x6784;"></p>
<hr>
<h2 id="&#x5F00;&#x53D1;&#x9636;&#x6BB5;&#x4EE3;&#x7801;&#x98CE;&#x683C;&#x63A7;&#x5236;-eslint"><a href="#&#x5F00;&#x53D1;&#x9636;&#x6BB5;&#x4EE3;&#x7801;&#x98CE;&#x683C;&#x63A7;&#x5236;-eslint" class="headerlink" title="&#x5F00;&#x53D1;&#x9636;&#x6BB5;&#x4EE3;&#x7801;&#x98CE;&#x683C;&#x63A7;&#x5236; eslint"></a>&#x5F00;&#x53D1;&#x9636;&#x6BB5;&#x4EE3;&#x7801;&#x98CE;&#x683C;&#x63A7;&#x5236; eslint</h2><p>&#x5B89;&#x88C5;&#xFF1A;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">$ npm install eslint -g</div><div class="line">$ npm install eslint-loader -save-dev</div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">module</span> : {</div><div class="line">  <span class="attr">preLoaders</span>: [</div><div class="line">      {<span class="attr">test</span>: <span class="regexp">/\.js$/</span>, <span class="attr">loader</span>: <span class="string">&quot;eslint-loader&quot;</span>, <span class="attr">exclude</span>: <span class="regexp">/node_modules/</span>}</div><div class="line">  ],</div><div class="line">}</div></pre></td></tr></table></figure>
<p>.eslintrc.js &#x6587;&#x4EF6;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">module</span>.exports = {</div><div class="line">    <span class="comment">// &#x5F00;&#x542F;&#x63A8;&#x8350;&#x914D;&#x7F6E;&#x4FE1;&#x606F;</span></div><div class="line">    <span class="comment">// &quot;extends&quot;: &quot;eslint:recommended&quot;,</span></div><div class="line">    <span class="comment">// &#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;ESLint &#x4F1A;&#x5728;&#x6240;&#x6709;&#x7236;&#x7EA7;&#x76EE;&#x5F55;&#x91CC;&#x5BFB;&#x627E;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x4E00;&#x76F4;&#x5230;&#x6839;&#x76EE;&#x5F55;&#x3002;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x8981;&#x4F60;&#x6240;&#x6709;&#x9879;&#x76EE;&#x90FD;&#x9075;&#x5FAA;&#x4E00;&#x4E2A;&#x7279;&#x5B9A;&#x7684;&#x7EA6;&#x5B9A;&#x65F6;&#xFF0C;&#x8FD9;&#x5C06;&#x4F1A;&#x5F88;&#x6709;&#x7528;&#xFF0C;&#x4F46;&#x6709;&#x65F6;&#x5019;&#x4F1A;&#x5BFC;&#x81F4;&#x610F;&#x60F3;&#x4E0D;&#x5230;&#x7684;&#x7ED3;&#x679C;&#x3002;&#x4E3A;&#x4E86;&#x5C06; ESLint &#x9650;&#x5236;&#x5230;&#x4E00;&#x4E2A;&#x7279;&#x5B9A;&#x7684;&#x9879;&#x76EE;&#xFF0C;&#x5728;&#x4F60;&#x9879;&#x76EE;&#x6839;&#x76EE;&#x5F55;&#x4E0B;&#x7684; package.json &#x6587;&#x4EF6;&#x6216;&#x8005; .eslintrc.* &#x6587;&#x4EF6;&#x91CC;&#x7684; eslintConfig &#x5B57;&#x6BB5;&#x4E0B;&#x8BBE;&#x7F6E; &quot;root&quot;: true&#x3002;ESLint &#x4E00;&#x65E6;&#x53D1;&#x73B0;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x6709; &quot;root&quot;: true&#xFF0C;&#x5B83;&#x5C31;&#x4F1A;&#x505C;&#x6B62;&#x5728;&#x7236;&#x7EA7;&#x76EE;&#x5F55;&#x4E2D;&#x5BFB;&#x627E;&#x3002;</span></div><div class="line">    <span class="string">&quot;root&quot;</span>: <span class="literal">true</span>,</div><div class="line">    <span class="comment">// &#x811A;&#x672C;&#x5728;&#x6267;&#x884C;&#x671F;&#x95F4;&#x8BBF;&#x95EE;&#x7684;&#x989D;&#x5916;&#x7684;&#x5168;&#x5C40;&#x53D8;&#x91CF;</span></div><div class="line">    <span class="comment">// &#x5F53;&#x8BBF;&#x95EE;&#x672A;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;no-undef &#x89C4;&#x5219;&#x5C06;&#x53D1;&#x51FA;&#x8B66;&#x544A;&#x3002;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x5728;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x91CC;&#x4F7F;&#x7528;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x63A8;&#x8350;&#x4F60;&#x5B9A;&#x4E49;&#x8FD9;&#x4E9B;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x8FD9;&#x6837; ESLint &#x5C31;&#x4E0D;&#x4F1A;&#x53D1;&#x51FA;&#x8B66;&#x544A;&#x4E86;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x6CE8;&#x91CA;&#x6216;&#x5728;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x3002;</span></div><div class="line">    <span class="string">&quot;globals&quot;</span> : {</div><div class="line">        <span class="string">&quot;window&quot;</span>:<span class="literal">true</span>,</div><div class="line">        <span class="string">&quot;document&quot;</span>:<span class="literal">true</span>,</div><div class="line">        <span class="string">&quot;$&quot;</span>:<span class="literal">true</span></div><div class="line">    },</div><div class="line">    <span class="comment">// &#x8BBE;&#x7F6E;&#x63D2;&#x4EF6;</span></div><div class="line">    <span class="comment">// &quot;plugins&quot;: [</span></div><div class="line">    <span class="comment">//     &apos;html&apos;</span></div><div class="line">    <span class="comment">// ],</span></div><div class="line">    <span class="comment">// &#x8BBE;&#x7F6E;&#x89E3;&#x6790;&#x5668;&#x9009;&#x9879;</span></div><div class="line">    <span class="string">&quot;parserOptions&quot;</span>: {</div><div class="line">        <span class="string">&quot;ecmaVersion&quot;</span>: <span class="number">6</span>,</div><div class="line">        <span class="string">&quot;sourceType&quot;</span>: <span class="string">&quot;module&quot;</span>,</div><div class="line">        <span class="string">&quot;ecmaFeatures&quot;</span>: {</div><div class="line">            <span class="string">&quot;jsx&quot;</span>: <span class="literal">true</span></div><div class="line">        }</div><div class="line">    },</div><div class="line">    <span class="comment">// &#x542F;&#x7528;&#x7684;&#x89C4;&#x5219;&#x53CA;&#x5404;&#x81EA;&#x7684;&#x9519;&#x8BEF;&#x7EA7;&#x522B;</span></div><div class="line">    <span class="string">&quot;rules&quot;</span> : {</div><div class="line">        <span class="comment">// &#x7981;&#x6B62;&#x7528;console</span></div><div class="line">        <span class="string">&quot;no-console&quot;</span>:<span class="number">1</span>,</div><div class="line">        <span class="comment">// &#x7981;&#x6B62;&#x7528;&#x5206;&#x53F7;</span></div><div class="line">        <span class="string">&quot;semi&quot;</span>:[<span class="number">1</span>,<span class="string">&apos;never&apos;</span>],</div><div class="line">        <span class="comment">// &#x5728;&#x540C;&#x4E00;&#x4E2A;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x7981;&#x6B62;&#x591A;&#x6B21;&#x91CD;&#x590D;&#x5B9A;&#x4E49;</span></div><div class="line">        <span class="string">&quot;no-redeclare&quot;</span>:<span class="number">1</span></div><div class="line">    },</div><div class="line">    <span class="comment">// &#x6307;&#x5B9A;&#x60F3;&#x542F;&#x7528;&#x7684;&#x73AF;&#x5883;</span></div><div class="line">    <span class="string">&quot;env&quot;</span>: {</div><div class="line">        <span class="string">&quot;browser&quot;</span>: <span class="literal">true</span>,</div><div class="line">        <span class="string">&quot;node&quot;</span>: <span class="literal">true</span></div><div class="line">    }</div><div class="line">};</div></pre></td></tr></table></figure>
<h2 id="&#x5176;&#x5B83;&#x77E5;&#x8BC6;&#x70B9;"><a href="#&#x5176;&#x5B83;&#x77E5;&#x8BC6;&#x70B9;" class="headerlink" title="&#x5176;&#x5B83;&#x77E5;&#x8BC6;&#x70B9;"></a>&#x5176;&#x5B83;&#x77E5;&#x8BC6;&#x70B9;</h2><h3 id="webpack&#x4E2D;&#x7684;&#x975E;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#xFF08;&#x5F02;&#x6B65;&#x52A0;&#x8F7D;&#xFF09;"><a href="#webpack&#x4E2D;&#x7684;&#x975E;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#xFF08;&#x5F02;&#x6B65;&#x52A0;&#x8F7D;&#xFF09;" class="headerlink" title="webpack&#x4E2D;&#x7684;&#x975E;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#xFF08;&#x5F02;&#x6B65;&#x52A0;&#x8F7D;&#xFF09;"></a>webpack&#x4E2D;&#x7684;&#x975E;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#xFF08;&#x5F02;&#x6B65;&#x52A0;&#x8F7D;&#xFF09;</h3><p>&#x8FD9;&#x4E2A;&#x662F;&#x91CD;&#x70B9;&#x8981;&#x914D;&#x5408; chunkname &#x5C5E;&#x6027;&#xFF0C;react-router &#x7684;&#x52A8;&#x6001;&#x8DEF;&#x7531;&#x4F1A;&#x7528;&#x5230;</p>
<p><a href="http://react-china.org/t/webpack-output-filename-output-chunkfilename/2256/2" target="_blank" rel="external">http://react-china.org/t/webpack-output-filename-output-chunkfilename/2256/2</a></p>
<p>&#x57FA;&#x672C;&#x4E0A;&#x90FD;&#x662F;&#x5728; require.ensure &#x53BB;&#x52A0;&#x8F7D;&#x6A21;&#x5757;&#x7684;&#x65F6;&#x5019;&#x624D;&#x4F1A;&#x51FA;&#x73B0;&#xFF0C;chunkFileName&#xFF0C;&#x4E2A;&#x4EBA;&#x7406;&#x89E3;&#x662F; cmd &#x548C; amd &#x5F02;&#x6B65;&#x52A0;&#x8F7D;&#x800C;&#x4E14;&#x6CA1;&#x6709;&#x7ED9;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x65F6;&#xFF0C;&#x4F1A;&#x751F;&#x6210;&#x4E86; no-name &#x7684; chunk&#xFF0C;&#x6240;&#x4EE5; chunkFileName&#x4E00;&#x822C;&#x90FD;&#x4F1A;&#x662F; [id].[chunkhash].js, &#x4E5F;&#x5C31;&#x662F;&#x8FD9;&#x79CD; chunk &#x7684;&#x547D;&#x540D;&#x4E00;&#x822C;&#x90FD;&#x4F1A;&#x662F; 0.a5898fnub6.js.</p>
<h3 id="Resolve&#x5C5E;&#x6027;"><a href="#Resolve&#x5C5E;&#x6027;" class="headerlink" title="Resolve&#x5C5E;&#x6027;"></a>Resolve&#x5C5E;&#x6027;</h3><p>webpack &#x5728;&#x6784;&#x5EFA;&#x5305;&#x7684;&#x65F6;&#x5019;&#x4F1A;&#x6309;&#x76EE;&#x5F55;&#x7684;&#x8FDB;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x67E5;&#x627E;&#xFF0C;resolve &#x5C5E;&#x6027;&#x4E2D;&#x7684; extensions &#x6570;&#x7EC4;&#x4E2D;&#x7528;&#x4E8E;&#x914D;&#x7F6E;&#x7A0B;&#x5E8F;&#x53EF;&#x4EE5;&#x81EA;&#x884C;&#x8865;&#x5168;&#x54EA;&#x4E9B;&#x6587;&#x4EF6;&#x540E;&#x7F00;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">resolve: {</div><div class="line">    <span class="comment">//&#x67E5;&#x627E;module&#x7684;&#x8BDD;&#x4ECE;&#x8FD9;&#x91CC;&#x5F00;&#x59CB;&#x67E5;&#x627E;</span></div><div class="line">    root: <span class="string">&apos;/pomy/github/flux-example/src&apos;</span>, <span class="comment">//&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</span></div><div class="line">    <span class="comment">//&#x81EA;&#x52A8;&#x6269;&#x5C55;&#x6587;&#x4EF6;&#x540E;&#x7F00;&#x540D;&#xFF0C;&#x610F;&#x5473;&#x7740;&#x6211;&#x4EEC;require&#x6A21;&#x5757;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x4E0D;&#x5199;&#x540E;&#x7F00;&#x540D;</span></div><div class="line">    <span class="comment">//&#x6CE8;&#x610F;&#x4E00;&#x4E0B;, extensions &#x7B2C;&#x4E00;&#x4E2A;&#x662F;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;! &#x5BF9;&#x5E94;&#x4E0D;&#x9700;&#x8981;&#x540E;&#x7F00;&#x7684;&#x60C5;&#x51B5;.</span></div><div class="line">    extensions: [<span class="string">&apos;&apos;</span>, <span class="string">&apos;.js&apos;</span>, <span class="string">&apos;.json&apos;</span>, <span class="string">&apos;.scss&apos;</span>,&#x2019;jsx&#x2019;],</div><div class="line"></div><div class="line">    <span class="comment">//&#x6A21;&#x5757;&#x522B;&#x540D;&#x5B9A;&#x4E49;&#xFF0C;&#x65B9;&#x4FBF;&#x540E;&#x7EED;&#x76F4;&#x63A5;&#x5F15;&#x7528;&#x522B;&#x540D;&#xFF0C;&#x65E0;&#x987B;&#x591A;&#x5199;&#x957F;&#x957F;&#x7684;&#x5730;&#x5740;</span></div><div class="line">    alias: {</div><div class="line">        <span class="attr">AppStore</span> : <span class="string">&apos;js/stores/AppStores.js&apos;</span>,<span class="comment">//&#x540E;&#x7EED;&#x76F4;&#x63A5; require(&apos;AppStore&apos;) &#x5373;&#x53EF;</span></div><div class="line">        ActionType : <span class="string">&apos;js/actions/ActionType.js&apos;</span>,</div><div class="line">        <span class="attr">AppAction</span> : <span class="string">&apos;js/actions/AppAction.js&apos;</span></div><div class="line">    }</div><div class="line">}</div></pre></td></tr></table></figure>
<h3 id="Externals&#x5C5E;&#x6027;"><a href="#Externals&#x5C5E;&#x6027;" class="headerlink" title="Externals&#x5C5E;&#x6027;"></a>Externals&#x5C5E;&#x6027;</h3><p>&#x5916;&#x90E8;&#x4F9D;&#x8D56;&#x4E0D;&#x9700;&#x8981;&#x6253;&#x5305;&#x8FDB; bundle&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x60F3;&#x5728;&#x9879;&#x76EE;&#x4E2D; require &#x4E00;&#x4E9B;&#x5176;&#x4ED6;&#x7684;&#x7C7B;&#x5E93;&#x6216;&#x8005; API &#xFF0C;&#x800C;&#x53C8;&#x4E0D;&#x60F3;&#x8BA9;&#x8FD9;&#x4E9B;&#x7C7B;&#x5E93;&#x7684;&#x6E90;&#x7801;&#x88AB;&#x6784;&#x5EFA;&#x5230;&#x8FD0;&#x884C;&#x65F6;&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x8FD9;&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#x5F88;&#x6709;&#x5FC5;&#x8981;&#x3002;<br>&#x6BD4;&#x5982;&#xFF1A;&#x5728;&#x9875;&#x9762;&#x91CC;&#x901A;&#x8FC7; script &#x6807;&#x7B7E;&#x5F15;&#x7528;&#x4E86; jQuery&#xFF1A;<code>&lt;script src=&quot;//code.jquery.com/jquery-1.12.0.min.js&quot;&gt;&lt;/script&gt;</code>&#xFF0C;&#x6240;&#x4EE5;&#x5E76;&#x4E0D;&#x60F3;&#x5728;&#x5176;&#x4ED6; js &#x91CC;&#x518D;&#x6253;&#x5305;&#x8FDB;&#x5165;&#x4E00;&#x904D;&#xFF0C;&#x6BD4;&#x5982;&#x4F60;&#x7684;&#x5176;&#x4ED6; js &#x4EE3;&#x7801;&#x7C7B;&#x4F3C;&#xFF1A;</p>
<p>&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x4E0D;&#x662F;&#x901A;&#x8FC7;require&#x6216;&#x8005;import&#x5F15;&#x5165;&#x7684;&#xFF0C;&#x800C;&#x662F;&#x76F4;&#x63A5;&#x5199;&#x5728;html&#x4E2D;&#x7684;js&#x5730;&#x5740;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x914D;&#x7F6E;&#x4E86;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x4E4B;&#x540E; react &#x548C; react-dom &#x8FD9;&#x4E9B;&#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x5305;&#x90FD;&#x4E0D;&#x4F1A;&#x88AB;&#x6784;&#x5EFA;&#x8FDB; js &#x4E2D;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x5C31;&#x9700;&#x8981;&#x901A;&#x8FC7; cdn &#x8FDB;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x5F15;&#x7528;&#x4E86;</span></div><div class="line"><span class="comment">// &#x524D;&#x8FB9;&#x7684;&#x8FD9;&#x4E2A;&#x540D;&#x79F0;&#x662F;&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x5F15;&#x7528;&#x7528;&#x7684;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E; import React from &apos;react1&apos; &#x4E2D;&#x7684; react</span></div><div class="line">externals: {</div><div class="line">    <span class="string">&apos;react1&apos;</span>: <span class="string">&apos;react&apos;</span>,</div><div class="line">    <span class="string">&apos;react-dom1&apos;</span>: <span class="string">&apos;react-dom&apos;</span>,</div><div class="line">    <span class="string">&apos;$1&apos;</span>: <span class="string">&apos;jQuery&apos;</span></div><div class="line">},</div></pre></td></tr></table></figure>
<p>&#x8FD9;&#x6837;&#x7528;&#x4E86; externals &#x5C5E;&#x6027;&#x65F6;&#x4E0D;&#x7528;&#x5206;&#x79BB;&#x63D2;&#x4EF6;&#x4E86;&#xFF0C;&#x4F5C;&#x7528;&#x662F;&#x8FD9;&#x91CC;&#x5F15;&#x7684;&#x63D2;&#x4EF6;&#x4E0D;&#x4F1A;&#x88AB; webpack &#x6240;&#x6253;&#x5305;&#x3002;&#x8981;&#x4E48;&#x7528; cdn &#x8981;&#x4E48;&#x9700;&#x8981; webpack &#x6253;&#x5305;&#x3002;&#x4E0B;&#x56FE;&#x4E3A; webpack &#x4E2D;&#x4F7F;&#x7528;&#x516C;&#x7528;&#x7684; CDN:</p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack014.png" alt="webpack &#x4E2D;&#x4F7F;&#x7528;&#x516C;&#x7528;&#x7684; CDN"></p>
<h3 id="&#x5F00;&#x53D1;&#x73AF;&#x5883;&#x4E2D;&#x4F7F;&#x7528;&#x538B;&#x7F29;&#x6587;&#x4EF6;"><a href="#&#x5F00;&#x53D1;&#x73AF;&#x5883;&#x4E2D;&#x4F7F;&#x7528;&#x538B;&#x7F29;&#x6587;&#x4EF6;" class="headerlink" title="&#x5F00;&#x53D1;&#x73AF;&#x5883;&#x4E2D;&#x4F7F;&#x7528;&#x538B;&#x7F29;&#x6587;&#x4EF6;"></a>&#x5F00;&#x53D1;&#x73AF;&#x5883;&#x4E2D;&#x4F7F;&#x7528;&#x538B;&#x7F29;&#x6587;&#x4EF6;</h3><p><a href="http://fakefish.github.io/react-webpack-cookbook/Optimizing-rebundling.html" target="_blank" rel="external">http://fakefish.github.io/react-webpack-cookbook/Optimizing-rebundling.html</a></p>
<p>&#x4E0D;&#x4F7F;&#x7528;&#x5C31;&#x4F1A;&#x628A; react &#x518D;&#x5904;&#x7406;&#x4E00;&#x904D;</p>
<h3 id="noParse&#x5C5E;&#x6027;"><a href="#noParse&#x5C5E;&#x6027;" class="headerlink" title="noParse&#x5C5E;&#x6027;"></a>noParse&#x5C5E;&#x6027;</h3><p>module.noParse &#x662F; webpack &#x7684;&#x53E6;&#x4E00;&#x4E2A;&#x5F88;&#x6709;&#x7528;&#x7684;&#x914D;&#x7F6E;&#x9879;&#xFF0C;&#x5982;&#x679C;&#x786E;&#x5B9A;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x4E2D;&#x6CA1;&#x6709;&#x5176;&#x4ED6;&#x65B0;&#x7684;&#x4F9D;&#x8D56;&#x9879;&#x5C31;&#x53EF;&#x4EE5;&#x914D;&#x7F6E;&#x8FD9;&#x4E2A;&#x50CF;&#xFF0C;webpack &#x5C06;&#x4E0D;&#x518D;&#x626B;&#x63CF;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x4F9D;&#x8D56;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">module</span>: {</div><div class="line">  <span class="attr">noParse</span>: [<span class="regexp">/moment-with-locales/</span>]</div><div class="line">}</div></pre></td></tr></table></figure>
<h3 id="&#x591A;&#x6587;&#x4EF6;&#x5165;&#x53E3;"><a href="#&#x591A;&#x6587;&#x4EF6;&#x5165;&#x53E3;" class="headerlink" title="&#x591A;&#x6587;&#x4EF6;&#x5165;&#x53E3;"></a>&#x591A;&#x6587;&#x4EF6;&#x5165;&#x53E3;</h3><p><a href="http://fakefish.github.io/react-webpack-cookbook/Multiple-entry-points.html" target="_blank" rel="external">http://fakefish.github.io/react-webpack-cookbook/Multiple-entry-points.html</a></p>
<p><img src="/2016/12/02/t13-webpack&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x5DE5;&#x5177;/webpack015.png" alt="&#x591A;&#x6587;&#x4EF6;&#x5165;&#x53E3;"></p>
<h3 id="&#x5F3A;&#x5236;&#x4ECE;&#x65B0;&#x52A0;&#x8F7D;&#x6587;&#x4EF6;"><a href="#&#x5F3A;&#x5236;&#x4ECE;&#x65B0;&#x52A0;&#x8F7D;&#x6587;&#x4EF6;" class="headerlink" title="&#x5F3A;&#x5236;&#x4ECE;&#x65B0;&#x52A0;&#x8F7D;&#x6587;&#x4EF6;"></a>&#x5F3A;&#x5236;&#x4ECE;&#x65B0;&#x52A0;&#x8F7D;&#x6587;&#x4EF6;</h3><p><a href="http://fakefish.github.io/react-webpack-cookbook/Optimizing-caching.html" target="_blank" rel="external">http://fakefish.github.io/react-webpack-cookbook/Optimizing-caching.html</a></p>
<h3 id="Chunk"><a href="#Chunk" class="headerlink" title="Chunk"></a>Chunk</h3><p>&#x4EE3;&#x7801;&#x5206;&#x79BB;&#xFF1A;</p>
<p><a href="http://webpack.github.io/docs/code-splitting.html" target="_blank" rel="external">http://webpack.github.io/docs/code-splitting.html</a></p>
<h3 id="&#x61D2;&#x52A0;&#x8F7D;"><a href="#&#x61D2;&#x52A0;&#x8F7D;" class="headerlink" title="&#x61D2;&#x52A0;&#x8F7D;"></a>&#x61D2;&#x52A0;&#x8F7D;</h3><p>(1)&#x3001;&#x5728; react &#x4E2D;&#x5982;&#x4F55;&#x4F7F;&#x7528;</p>
<p><a href="http://fakefish.github.io/react-webpack-cookbook/Lazy-loaded-entry-points.html" target="_blank" rel="external">http://fakefish.github.io/react-webpack-cookbook/Lazy-loaded-entry-points.html</a></p>
<p>(2)&#x3001;&#x5728; react-router &#x4E2D;&#x7528;&#x5230;&#x52A8;&#x6001;&#x52A0;&#x8F7D;&#x8DEF;&#x7531;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;</p>
<h3 id="&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x7528;-webpack"><a href="#&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x7528;-webpack" class="headerlink" title="&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x7528; webpack"></a>&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x7528; webpack</h3><p>Node &#x548C;webpack &#x96C6;&#x6210;&#x7528;&#x5230;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#xFF1A;<a href="http://www.tuicool.com/articles/IvQb2ey" target="_blank" rel="external">http://www.tuicool.com/articles/IvQb2ey</a><br>Node &#x548C;webpack &#x96C6;&#x6210;&#x8FC7;&#x7A0B;&#x4E2D;&#x9047;&#x5230;&#x7684;&#x5751;&#x5982;&#x4F55;&#x89E3;&#x51B3;&#xFF1A;<a href="http://www.tuicool.com/articles/zEZneuq" target="_blank" rel="external">http://www.tuicool.com/articles/zEZneuq</a></p>
<p>&#x4E0D;&#x63A8;&#x8350;&#x7528; webpack &#x6784;&#x5EFA; Node &#x4EE3;&#x7801;</p>
<h3 id="&#x70ED;&#x52A0;&#x8F7D;&#x7EC4;&#x4EF6;"><a href="#&#x70ED;&#x52A0;&#x8F7D;&#x7EC4;&#x4EF6;" class="headerlink" title="&#x70ED;&#x52A0;&#x8F7D;&#x7EC4;&#x4EF6;"></a>&#x70ED;&#x52A0;&#x8F7D;&#x7EC4;&#x4EF6;</h3><p><a href="http://fakefish.github.io/react-webpack-cookbook/Hot-loading-components.html" target="_blank" rel="external">http://fakefish.github.io/react-webpack-cookbook/Hot-loading-components.html</a></p>
<h2 id="&#x6700;&#x540E;&#x7684;-package-json"><a href="#&#x6700;&#x540E;&#x7684;-package-json" class="headerlink" title="&#x6700;&#x540E;&#x7684; package.json"></a>&#x6700;&#x540E;&#x7684; package.json</h2><figure class="highlight json"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line">{</div><div class="line">  <span class="attr">&quot;name&quot;</span>: <span class="string">&quot;webpack_example&quot;</span>,</div><div class="line">  <span class="attr">&quot;version&quot;</span>: <span class="string">&quot;1.0.0&quot;</span>,</div><div class="line">  <span class="attr">&quot;description&quot;</span>: <span class="string">&quot;CommonJS &#x548C; AMD &#x662F;&#x7528;&#x4E8E; JavaScript &#x6A21;&#x5757;&#x7BA1;&#x7406;&#x7684;&#x4E24;&#x5927;&#x89C4;&#x8303;&#xFF0C;&#x524D;&#x8005;&#x5B9A;&#x4E49;&#x7684;&#x662F;&#x6A21;&#x5757;&#x7684;&#x540C;&#x6B65;&#x52A0;&#x8F7D;&#xFF0C;&#x4E3B;&#x8981;&#x7528;&#x4E8E; NodeJS &#xFF1B;&#x800C;&#x540E;&#x8005;&#x5219;&#x662F;&#x5F02;&#x6B65;&#x52A0;&#x8F7D;&#xFF0C;&#x901A;&#x8FC7; RequireJS &#x7B49;&#x5DE5;&#x5177;&#x9002;&#x7528;&#x4E8E;&#x524D;&#x7AEF;&#x3002;&#x968F;&#x7740; npm &#x6210;&#x4E3A;&#x4E3B;&#x6D41;&#x7684; JavaScript &#x7EC4;&#x4EF6;&#x53D1;&#x5E03;&#x5E73;&#x53F0;&#xFF0C;&#x8D8A;&#x6765;&#x8D8A;&#x591A;&#x7684;&#x524D;&#x7AEF;&#x9879;&#x76EE;&#x4E5F;&#x4F9D;&#x8D56;&#x4E8E; npm &#x4E0A;&#x7684;&#x9879;&#x76EE;&#xFF0C;&#x6216;&#x8005;&#x81EA;&#x8EAB;&#x5C31;&#x4F1A;&#x53D1;&#x5E03;&#x5230; npm &#x5E73;&#x53F0;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x8BA9;&#x524D;&#x7AEF;&#x9879;&#x76EE;&#x66F4;&#x65B9;&#x4FBF;&#x7684;&#x4F7F;&#x7528; npm &#x4E0A;&#x7684;&#x8D44;&#x6E90;&#x6210;&#x4E3A;&#x4E00;&#x5927;&#x9700;&#x6C42;&#x3002;&quot;</span>,</div><div class="line">  <span class="attr">&quot;main&quot;</span>: <span class="string">&quot;index.js&quot;</span>,</div><div class="line">  <span class="attr">&quot;scripts&quot;</span>: {</div><div class="line">    <span class="attr">&quot;test&quot;</span>: <span class="string">&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span>,</div><div class="line">    <span class="attr">&quot;develop&quot;</span>: <span class="string">&quot;webpack-dev-server --config webpack.develop.config.js --devtool eval --progress --colors --hot --content-base src&quot;</span>,</div><div class="line">    <span class="attr">&quot;publish&quot;</span>: <span class="string">&quot;webpack --config webpack.publish.config.js&quot;</span></div><div class="line">  },</div><div class="line">  <span class="attr">&quot;keywords&quot;</span>: [],</div><div class="line">  <span class="attr">&quot;author&quot;</span>: <span class="string">&quot;&quot;</span>,</div><div class="line">  <span class="attr">&quot;license&quot;</span>: <span class="string">&quot;ISC&quot;</span>,</div><div class="line">  <span class="attr">&quot;devDependencies&quot;</span>: {</div><div class="line">    <span class="attr">&quot;babel-core&quot;</span>: <span class="string">&quot;^6.20.0&quot;</span>,</div><div class="line">    <span class="attr">&quot;babel-loader&quot;</span>: <span class="string">&quot;^6.2.9&quot;</span>,</div><div class="line">    <span class="attr">&quot;babel-preset-es2015&quot;</span>: <span class="string">&quot;^6.18.0&quot;</span>,</div><div class="line">    <span class="attr">&quot;babel-preset-react&quot;</span>: <span class="string">&quot;^6.16.0&quot;</span>,</div><div class="line">    <span class="attr">&quot;css-loader&quot;</span>: <span class="string">&quot;^0.26.1&quot;</span>,</div><div class="line">    <span class="attr">&quot;eslint&quot;</span>: <span class="string">&quot;^3.12.0&quot;</span>,</div><div class="line">    <span class="attr">&quot;eslint-loader&quot;</span>: <span class="string">&quot;^1.6.1&quot;</span>,</div><div class="line">    <span class="attr">&quot;extract-text-webpack-plugin&quot;</span>: <span class="string">&quot;^1.0.1&quot;</span>,</div><div class="line">    <span class="attr">&quot;file-loader&quot;</span>: <span class="string">&quot;^0.9.0&quot;</span>,</div><div class="line">    <span class="attr">&quot;gulp&quot;</span>: <span class="string">&quot;^3.9.1&quot;</span>,</div><div class="line">    <span class="attr">&quot;html-webpack-plugin&quot;</span>: <span class="string">&quot;^2.24.1&quot;</span>,</div><div class="line">    <span class="attr">&quot;node-sass&quot;</span>: <span class="string">&quot;^4.0.0&quot;</span>,</div><div class="line">    <span class="attr">&quot;open-browser-webpack-plugin&quot;</span>: <span class="string">&quot;0.0.3&quot;</span>,</div><div class="line">    <span class="attr">&quot;sass-loader&quot;</span>: <span class="string">&quot;^4.0.2&quot;</span>,</div><div class="line">    <span class="attr">&quot;style-loader&quot;</span>: <span class="string">&quot;^0.13.1&quot;</span>,</div><div class="line">    <span class="attr">&quot;url-loader&quot;</span>: <span class="string">&quot;^0.5.7&quot;</span>,</div><div class="line">    <span class="attr">&quot;webpack&quot;</span>: <span class="string">&quot;^1.14.0&quot;</span>,</div><div class="line">    <span class="attr">&quot;webpack-dev-server&quot;</span>: <span class="string">&quot;^1.16.2&quot;</span></div><div class="line">  },</div><div class="line">  <span class="attr">&quot;dependencies&quot;</span>: {</div><div class="line">    <span class="attr">&quot;react&quot;</span>: <span class="string">&quot;^15.4.1&quot;</span>,</div><div class="line">    <span class="attr">&quot;react-dom&quot;</span>: <span class="string">&quot;^15.4.1&quot;</span></div><div class="line">  }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x5730;&#x5740;&#xFF1A;<a href="https://github.com/HongqinMa/webpack_example" target="_blank" rel="external">https://github.com/HongqinMa/webpack_example</a></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>感谢您的支持！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>打 赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="http://ofy7k3v4s.bkt.clouddn.com/wcpayCode.jpg" alt="Hongqin Ma WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="http://ofy7k3v4s.bkt.clouddn.com/apayCode.jpg" alt="Hongqin Ma Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/tools/" rel="tag">#tools</a>
          
            <a href="/tags/webpack/" rel="tag">#webpack</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/11/24/t12-混合APP开发环境配置/" rel="next" title="混合APP开发环境配置-ionic、cordora">
                <i class="fa fa-chevron-left"></i> 混合APP开发环境配置-ionic、cordora
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/12/05/s015-React学习笔记/" rel="prev" title="React学习笔记">
                React学习笔记 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2016/12/02/t13-webpack项目构建工具/"
     data-title="webpack 构建工具"
     data-content=""
     data-url="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/12/02/t13-webpack项目构建工具/"
           data-title="webpack 构建工具" data-url="https://hongqinma.github.io/2016/12/02/t13-webpack项目构建工具/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/photo.jpg"
               alt="Hongqin Ma" />
          <p class="site-author-name" itemprop="name">Hongqin Ma</p>
          <p class="site-description motion-element" itemprop="description">Spell at all costs， to rush you in the future. </p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">68</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">16</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">57</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/HongqinMa" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/HongqinMa" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.facebook.com/profile.php?id=100006581058735" target="_blank" title="Facebook">
                  
                    <i class="fa fa-fw fa-facebook"></i>
                  
                  Facebook
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/HirakuMa" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-share-alt"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/rubylouvre/" title="司徒正美" target="_blank">司徒正美</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/sanshi/" title="三生石上" target="_blank">三生石上</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/TomXu/" title="汤姆大叔" target="_blank">汤姆大叔</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://gold.xitu.io/" title="掘金" target="_blank">掘金</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://segmentfault.com/" title="segmentfault" target="_blank">segmentfault</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.jianshu.com/users/1cbb94e5ed4d/latest_articles" title="简书" target="_blank">简书</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://home.cnblogs.com/" title="博客园" target="_blank">博客园</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://www.awesomes.cn/" title="Awesomes" target="_blank">Awesomes</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#webpack-介绍"><span class="nav-number">1.</span> <span class="nav-text">webpack 介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#webpack-和-Gulp-的区别"><span class="nav-number">1.1.</span> <span class="nav-text">webpack 和 Gulp 的区别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#webpack-官网文档"><span class="nav-number">1.2.</span> <span class="nav-text">webpack 官网文档</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#webpack-的优势"><span class="nav-number">1.3.</span> <span class="nav-text">webpack 的优势</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#webpack-的使用"><span class="nav-number">2.</span> <span class="nav-text">webpack 的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#搭建基本的项目结构如下图："><span class="nav-number">2.1.</span> <span class="nav-text">搭建基本的项目结构如下图：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#安装"><span class="nav-number">2.2.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配置文件"><span class="nav-number">2.3.</span> <span class="nav-text">配置文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#运行"><span class="nav-number">2.4.</span> <span class="nav-text">运行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#进行版本控制"><span class="nav-number">2.5.</span> <span class="nav-text">进行版本控制</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#webpack-启动过程演进"><span class="nav-number">3.</span> <span class="nav-text">webpack 启动过程演进</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#更好的方式实现动启动"><span class="nav-number">3.1.</span> <span class="nav-text">更好的方式实现动启动</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#浏览器自动刷新"><span class="nav-number">3.2.</span> <span class="nav-text">浏览器自动刷新</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#常用加载器"><span class="nav-number">4.</span> <span class="nav-text">常用加载器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#编译-JSX-和-ES6-到-ES5-语法的加载器"><span class="nav-number">4.1.</span> <span class="nav-text">编译 JSX 和 ES6 到 ES5 语法的加载器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#加载-CSS"><span class="nav-number">4.2.</span> <span class="nav-text">加载 CSS</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#安装-1"><span class="nav-number">4.2.1.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#修改配置文件："><span class="nav-number">4.2.2.</span> <span class="nav-text">修改配置文件：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Css加载策略"><span class="nav-number">4.2.3.</span> <span class="nav-text">Css加载策略</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#加载sass"><span class="nav-number">4.3.</span> <span class="nav-text">加载sass</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#图片处理"><span class="nav-number">4.4.</span> <span class="nav-text">图片处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#components-Hello-js"><span class="nav-number">4.5.</span> <span class="nav-text">components/Hello.js</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#components-base-scss"><span class="nav-number">4.6.</span> <span class="nav-text">components/_base.scss</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#components-Hello-css"><span class="nav-number">4.7.</span> <span class="nav-text">components/Hello.css</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#components-Hello-scss"><span class="nav-number">4.8.</span> <span class="nav-text">components/Hello.scss</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#最终的-webpack-develop-config-js-文件"><span class="nav-number">4.9.</span> <span class="nav-text">最终的 webpack.develop.config.js 文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#webpack-的部署策略"><span class="nav-number">5.</span> <span class="nav-text">webpack 的部署策略</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#修改-npm-的-package-json-文件"><span class="nav-number">5.1.</span> <span class="nav-text">修改 npm 的 package.json 文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#分离应用和第三方"><span class="nav-number">5.2.</span> <span class="nav-text">分离应用和第三方</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#和-gulp-的集成"><span class="nav-number">5.3.</span> <span class="nav-text">和 gulp 的集成</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#合并成单文件"><span class="nav-number">5.4.</span> <span class="nav-text">合并成单文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#常用插件"><span class="nav-number">6.</span> <span class="nav-text">常用插件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#压缩插件-webpack-optimize-UglifyJsPlugin-这个插件是webpack自带的"><span class="nav-number">6.1.</span> <span class="nav-text">压缩插件 webpack.optimize.UglifyJsPlugin, 这个插件是webpack自带的.</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#提取css插件"><span class="nav-number">6.2.</span> <span class="nav-text">提取css插件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自动创建-index-Html-页面插件"><span class="nav-number">6.3.</span> <span class="nav-text">自动创建 index.Html 页面插件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#优化第三方包"><span class="nav-number">6.4.</span> <span class="nav-text">优化第三方包</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自动打开浏览器插件"><span class="nav-number">6.5.</span> <span class="nav-text">自动打开浏览器插件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#提取-js-公共部分插件"><span class="nav-number">6.6.</span> <span class="nav-text">提取 js 公共部分插件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ProvidePlugin插件"><span class="nav-number">6.7.</span> <span class="nav-text">ProvidePlugin插件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除目录插件"><span class="nav-number">6.8.</span> <span class="nav-text">删除目录插件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#拷贝文件插件"><span class="nav-number">6.9.</span> <span class="nav-text">拷贝文件插件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#合并配置文件插件"><span class="nav-number">6.10.</span> <span class="nav-text">合并配置文件插件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#最终的-webpack-publish-config-js-文件"><span class="nav-number">6.11.</span> <span class="nav-text">最终的 webpack.publish.config.js 文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#开发阶段代码风格控制-eslint"><span class="nav-number">7.</span> <span class="nav-text">开发阶段代码风格控制 eslint</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#其它知识点"><span class="nav-number">8.</span> <span class="nav-text">其它知识点</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#webpack中的非入口文件（异步加载）"><span class="nav-number">8.1.</span> <span class="nav-text">webpack中的非入口文件（异步加载）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Resolve属性"><span class="nav-number">8.2.</span> <span class="nav-text">Resolve属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Externals属性"><span class="nav-number">8.3.</span> <span class="nav-text">Externals属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#开发环境中使用压缩文件"><span class="nav-number">8.4.</span> <span class="nav-text">开发环境中使用压缩文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#noParse属性"><span class="nav-number">8.5.</span> <span class="nav-text">noParse属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多文件入口"><span class="nav-number">8.6.</span> <span class="nav-text">多文件入口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#强制从新加载文件"><span class="nav-number">8.7.</span> <span class="nav-text">强制从新加载文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Chunk"><span class="nav-number">8.8.</span> <span class="nav-text">Chunk</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#懒加载"><span class="nav-number">8.9.</span> <span class="nav-text">懒加载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在服务器端用-webpack"><span class="nav-number">8.10.</span> <span class="nav-text">在服务器端用 webpack</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#热加载组件"><span class="nav-number">8.11.</span> <span class="nav-text">热加载组件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#最后的-package-json"><span class="nav-number">9.</span> <span class="nav-text">最后的 package.json</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Hongqin Ma</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span id="busuanzi_container_site_pv">
    &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;Page Views: <span id="busuanzi_value_site_pv"></span> 
</span>

        

<div class="busuanzi-count">

  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv"><i class="fa fa-user"></i><span class="busuanzi-value" id="busuanzi_value_site_uv"></span></span>
  

  
    <span class="site-pv"><i class="fa fa-eye"></i><span class="busuanzi-value" id="busuanzi_value_site_pv"></span></span>
  
  
</div>



        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"Hiraku"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  <!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
<script>
function showTime(Counter) {
  var query = new AV.Query(Counter);
  $(".leancloud_visitors").each(function() {
    var url = $(this).attr("id").trim();
    query.equalTo("url", url);
    query.find({
      success: function(results) {
        if (results.length == 0) {
          var content = '0 ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
          return;
        }
        for (var i = 0; i < results.length; i++) {
          var object = results[i];
          var content = object.get('time') + ' ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
        }
      },
      error: function(object, error) {
        console.log("Error: " + error.code + " " + error.message);
      }
    });

  });
}

function addCount(Counter) {
  var Counter = AV.Object.extend("Counter");
  url = $(".leancloud_visitors").attr('id').trim();
  title = $(".leancloud_visitors").attr('data-flag-title').trim();
  var query = new AV.Query(Counter);
  query.equalTo("url", url);
  query.find({
    success: function(results) {
      if (results.length > 0) {
        var counter = results[0];
        counter.fetchWhenSave(true);
        counter.increment("time");
        counter.save(null, {
          success: function(counter) {
            var content =  counter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(counter, error) {
            console.log('Failed to save Visitor num, with error message: ' + error.message);
          }
        });
      } else {
        var newcounter = new Counter();
        newcounter.set("title", title);
        newcounter.set("url", url);
        newcounter.set("time", 1);
        newcounter.save(null, {
          success: function(newcounter) {
              console.log("newcounter.get('time')="+newcounter.get('time'));
            var content = newcounter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(newcounter, error) {
            console.log('Failed to create');
          }
        });
      }
    },
    error: function(error) {
      console.log('Error:' + error.code + " " + error.message);
    }
  });
}
$(function() {
  var Counter = AV.Object.extend("Counter");
  if ($('.leancloud_visitors').length == 1) {
    addCount(Counter);
  } else if ($('.post-title-link').length > 1) {
    showTime(Counter);
  }
}); 
</script>

  
</body>
</html>
